Alright, time to get your hands dirty. Before you can write your first test, you need to add JUnit 5 to your project. The good news is that this is dead simple with Maven or Gradle. Let us walk through both.
Setting Up with Maven
If you are using Maven, open your pom.xml and add the following dependencies:
<dependencies>
<!-- JUnit Jupiter API -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<!-- JUnit Jupiter Engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
The scope is set to test because we only need these libraries
during testing, not when our application runs in production. The maven-surefire-plugin
is what actually discovers and runs your tests when you execute mvn test.
Setting Up with Gradle
If you prefer Gradle, open your build.gradle and add:
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.2'
}
test {
useJUnitPlatform()
}
The testImplementation configuration means "only include this when compiling
test code." The testRuntimeOnly means "only include this at runtime for
tests." And useJUnitPlatform() tells Gradle to use JUnit 5 as the test runner.
Project Structure
Once you have JUnit added, make sure your project follows the standard Maven/Gradle directory structure:
my-project/
โโโ src/
โ โโโ main/
โ โ โโโ java/
โ โ โโโ com/example/
โ โ โโโ Calculator.java <-- Your code
โ โโโ test/
โ โโโ java/
โ โโโ com/example/
โ โโโ CalculatorTest.java <-- Your tests
โโโ pom.xml (or build.gradle)
Notice that the test class has the same package as the class it tests, but lives
under src/test instead of src/main. This is important
because it gives your tests access to package-private members if needed.
Verifying the Setup
Let us write a quick test to make sure everything works. Create a test file:
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CalculatorTest {
@Test
void shouldReturnSum() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3));
}
}
Now run your tests:
# Maven
mvn test
# Gradle
gradle test
If everything is set up correctly, you should see a green bar or a message saying
BUILD SUCCESS. If you see a red bar, double-check your dependencies
and make sure the test file is in the right directory.
IDE Support
Most modern Java IDEs have built-in support for JUnit 5:
- IntelliJ IDEA - Right-click the test class and select "Run"
- Eclipse - Right-click the test class and select "Run As > JUnit Test"
- VS Code - Install the "Java Test Runner" extension
IDEs will automatically detect the @Test annotation and let you run
individual tests or entire test suites with a single click.