JUnit is a popular Java testing framework that helps developers write and run repeatable tests. It provides annotations and assertions to simplify testing and ensure that the code behaves as expected. This guide will walk you through the steps to set up a JUnit test, from creating a project to running your tests.
Step 1: Create a Project
1.1 Using IntelliJ IDEA IDE
- Create a New Project: Open IntelliJ IDEA and select
New Project. - Select Project Type: Choose
Javaand thenNext. - Configure Project: Name your project and specify the location, then click
Finish.
1.2 Using Eclipse
- Create a New Project: Open Eclipse and select
File>New>Java Project. - Configure Project: Name your project and specify the location, then click
Finish.
Step 2: Add JUnit Dependency
Maven
Add the JUnit dependency to the pom.xml file:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.3</version>
</dependency>
</dependencies>
Gradle
Add the JUnit dependency to the build.gradle file:
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.3'
}
test {
useJUnitPlatform()
}
Step 3: Create a Class Under Test
Create a Java class that contains the methods you want to test.
Example: Calculator Class
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
Step 4: Create a Test Class
Create a test class that will contain your test methods.
Example: CalculatorTest Class
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CalculatorTest {
private final Calculator calculator = new Calculator();
@Test
void testAdd() {
assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
}
@Test
void testSubtract() {
assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
}
@Test
void testMultiply() {
assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
}
@Test
void testDivide() {
assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
}
@Test
void testDivideByZero() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
calculator.divide(1, 0);
});
assertEquals("Division by zero", exception.getMessage());
}
}
Step 5: Run the Tests
To run the tests, you can use an IDE like IntelliJ IDEA or Eclipse or a build tool like Maven or Gradle.
Using IntelliJ IDEA
- Run Tests: Right-click on the
CalculatorTestfile and selectRun 'CalculatorTest'. - View Results: The results will be displayed in the Run window, showing which tests passed and which failed.

Using Eclipse
- Run Tests: Right-click on the
CalculatorTestfile and selectRun As>JUnit Test. - View Results: The results will be displayed in the JUnit view, showing which tests passed and which failed.
Key Concepts
- Test Class: A Java class that contains one or more test methods.
- Test Method: A method annotated with
@Testthat contains assertions to verify the expected outcomes. - Assertions: Methods used to check whether the code under test behaves as expected (e.g.,
assertEquals,assertTrue). - Test Runner: A tool provided by an IDE or build tool that discovers and executes test methods and reports the results.
Conclusion
You have successfully written and run your first JUnit test. This process involves creating a Maven project, creating a simple Java class, writing a test class with test methods, and running the test to verify that your code works as expected. This foundational skill will enable you to write more comprehensive tests for your Java applications.