Sub topic 2
Setting Up JUnit 5 in Your Project
Before you can write a single test, your project needs to know about JUnit. This chapter shows you how to add JUnit 5 to a Java project that uses Maven or Gradle. It also explains where test files go, how to check that everything works, and how to fix the most common setup problems.
What You Are Adding
JUnit 5 is a library. A library is code that someone else wrote, packaged, and shared so you can use it in your own project. You do not download it by hand. Your build tool downloads it for you.
A build tool is a program that compiles your code, downloads libraries, and runs your tests. The two most popular build tools for Java are Maven and Gradle. You only need one of them. Pick the one your project already uses.
JUnit 5 is made of three parts:
| Part | What it does |
|---|---|
| JUnit Platform | The base layer. It finds tests and runs them. IDEs and build tools talk to it. |
| JUnit Jupiter | The part you write tests with. It gives you `@Test`, assertions, and the other annotations in this tutorial. |
| JUnit Vintage | An optional bridge that runs old JUnit 3 and JUnit 4 tests on the new platform. |
An annotation is a label that starts with @, such as @Test. It gives extra information to a tool. JUnit reads annotations to decide which methods are tests.
For new projects you need the Platform and Jupiter. You do not need Vintage unless you also have old JUnit 4 tests.
Where Test Code Lives
Maven and Gradle both expect the same folder layout. Your normal code, called production code, goes in one folder. Your tests go in another.
my-project/
├── pom.xml (Maven) or build.gradle.kts (Gradle)
└── src/
├── main/
│ └── java/
│ └── Calculator.java
└── test/
└── java/
└── CalculatorTest.javaProduction code is the code that ships to users. Test code is never shipped. Keeping the two apart means JUnit stays out of your final program.
Option 1: Maven
Maven reads a file named pom.xml in the project's top folder. You list your libraries in its <dependencies> section. A dependency is a library your project needs.
Add this dependency:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>Here is what each line means:
groupIdnames the organization that publishes the library.artifactIdnames the library itself.junit-jupiteris a bundle. It pulls in the Jupiter API, support for parameterized tests, and the engine that runs tests.versionpicks the release. This tutorial uses 5.11.3.scopeset totestmeans JUnit is only used when compiling and running tests. It is left out of your final program.
A complete pom.xml
Maven runs tests with a plugin called Surefire. A plugin adds a task to the build tool. Old Surefire versions cannot find JUnit 5 tests, so set a recent version yourself. Here is a full file you can copy:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>calculator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
</project>Option 2: Gradle
Gradle reads a build file. This tutorial uses the Kotlin version of that file, named build.gradle.kts. Add these lines:
dependencies {
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.test {
useJUnitPlatform()
}Here is what each line means:
platform("org.junit:junit-bom:5.11.3")imports a BOM. A BOM (bill of materials) is a list of library versions that are known to work together. Because of it, the next two lines do not need their own version numbers.testImplementationmakes JUnit available when you compile and run tests, and nowhere else.testRuntimeOnlyadds the launcher. The launcher is the piece Gradle uses to start the JUnit Platform. It is only needed while tests run.useJUnitPlatform()tells Gradle to run tests on the JUnit Platform. Without this line, Gradle looks for JUnit 4 tests and finds none.
A complete build.gradle.kts
plugins {
java
}
group = "com.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.test {
useJUnitPlatform()
}The repositories block tells Gradle where to download libraries from. Maven Central is the main public store for Java libraries. Maven uses it by default, so the pom.xml did not need this block.
Check That Setup Works
A smoke test is a tiny test that only checks whether things start up at all. Create this file at src/test/java/com/example/SetupCheckTest.java:
package com.example;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class SetupCheckTest {
@Test
void junitIsWorking() {
assertTrue(true);
}
}The next chapter explains what each line does. For now, run the tests from a terminal in the project's top folder:
| Build tool | Command |
|---|---|
| Maven | `mvn test` |
| Gradle (macOS or Linux) | `./gradlew test` |
| Gradle (Windows) | `gradlew.bat test` |
With Maven, a working setup prints a line like this:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESSGradle prints BUILD SUCCESSFUL. To see each test, open the report at build/reports/tests/test/index.html.
Once this works, you can delete SetupCheckTest.
Common Setup Problems
| What you see | Likely cause | Fix |
|---|---|---|
| `package org.junit.jupiter.api does not exist` | The dependency is missing, or your test file is in `src/main/java`. | Add the dependency. Move the test to `src/test/java`. |
| Maven says `Tests run: 0` | Surefire is too old. | Set `maven-surefire-plugin` to version 3.x. |
| Gradle runs no tests and shows no error | `useJUnitPlatform()` is missing. | Add the `tasks.test { useJUnitPlatform() }` block. |
| Gradle error about the launcher, such as "Failed to load JUnit Platform" | The launcher dependency is missing. | Add the `testRuntimeOnly` launcher line. |
| Your IDE underlines `@Test` in red | The IDE has not reloaded the build file. | Use "Reload Maven project" or "Sync Gradle" in your IDE. |
| Tests import `org.junit.Test` | That class belongs to JUnit 4. | Import `org.junit.jupiter.api.Test` instead. |
| Your test class is not run | Its name does not end in `Test` or `Tests`. | Rename it, for example to `CalculatorTest`. |
The last row matters for Maven. By default, Surefire only runs classes whose names start with Test or end with Test, Tests, or TestCase. Gradle scans every class, but sticking to the ...Test naming habit keeps both tools happy.
Summary
- JUnit 5 has three parts: the Platform runs tests, Jupiter is what you write tests with, and Vintage is an optional bridge for old JUnit 4 tests.
- Test code goes in
src/test/java. Production code goes insrc/main/java. - In Maven, add
junit-jupiterwithtestscope and use Surefire 3.x. - In Gradle, import the JUnit BOM, add
junit-jupiterand the launcher, and calluseJUnitPlatform(). - Run a smoke test with
mvn testor./gradlew testto confirm the setup works.
Exercise
You add the junit-jupiter dependency to a Gradle project, write a test, and run ./gradlew test. The build succeeds, but the report shows zero tests. What line is most likely missing, and why does it matter?
Show answer
The tasks.test { useJUnitPlatform() } block is most likely missing. Without it, Gradle looks for JUnit 4 tests by default. It never starts the JUnit Platform, so it never finds your JUnit 5 tests, and the build "passes" with nothing run.
Exercise
Why is the Maven dependency given <scope>test</scope> instead of the default scope?
Show answer
Test scope makes JUnit available only when Maven compiles and runs test code in src/test/java. JUnit is left out of the production code and out of the packaged program, which keeps the program smaller and stops production code from accidentally depending on testing classes.
Exercise
A teammate puts CalculatorTest.java in src/main/java/com/example and gets "package org.junit.jupiter.api does not exist" when building. The dependency is in pom.xml with test scope. What is wrong?
Show answer
Test-scoped libraries are only visible to code under src/test/java. Because the test file is in src/main/java, Maven compiles it as production code, where JUnit is not available. Moving the file to src/test/java/com/example fixes the error.
Exercise
In the Gradle file, the line testImplementation("org.junit.jupiter:junit-jupiter") has no version number. How does Gradle know which version to download?
Show answer
The line testImplementation(platform("org.junit:junit-bom:5.11.3")) imports the JUnit BOM. The BOM lists matching versions for all JUnit libraries, so Gradle takes the junit-jupiter version, and the launcher version, from it.