Sub topic 2
Setting Up JUnit 5 in Your Project
Before you can write a test, your project needs the JUnit 5 library. This chapter shows how to add it with Maven or Gradle and where your test files should go.
A Few Terms First
- A build tool is a program that compiles your code, downloads libraries, and runs your tests. Maven and Gradle are the two most common build tools for Java.
- A dependency is a library your project uses. You list it in a build file, and the build tool downloads it for you.
- JUnit Jupiter is the part of JUnit 5 you write tests with. It gives you annotations such as
@Test. - The JUnit Platform is the part of JUnit 5 that finds your tests and runs them.
Option 1: Maven
Maven reads a file called pom.xml in the root folder of your project. Add this block inside the <dependencies> section:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>The junit-jupiter artifact brings in three things: the API you write tests with, support for parameterized tests, and the engine that runs them.
The line <scope>test</scope> sets the dependency's scope, which controls where the library can be used. A test scope means JUnit is available only to test code. It is left out of the program you ship to users.
Option 2: Gradle
Gradle reads a build file. If you use the Kotlin language for it, the file is called build.gradle.kts. Add this:
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 does:
| Line | Purpose |
|---|---|
| `platform("org.junit:junit-bom:5.11.3")` | Imports a BOM (bill of materials), a list that picks matching versions for all JUnit parts |
| `junit-jupiter` | Adds the test API and engine. It needs no version because the BOM supplies one |
| `junit-platform-launcher` | Lets Gradle start the JUnit Platform when tests run |
| `useJUnitPlatform()` | Tells Gradle to run tests with JUnit 5 |
Where Test Files Go
Both Maven and Gradle expect the same folder layout. Your normal code goes in src/main/java, and your tests go in src/test/java. Put each test class in the same package as the class it tests. That way the test can reach methods that are not marked public.
Check Your Setup
Run the test command from your project folder:
- Maven:
mvn test - Gradle:
./gradlew test
If the build downloads JUnit and finishes without errors, your setup works. You have no tests yet, so nothing gets tested. In the next chapter you will write your first one.
Exercise
You add JUnit to a Gradle project, write a test, and run ./gradlew test. The build succeeds, but your test never runs. What is the most likely cause?
Show answer
The tasks.test { useJUnitPlatform() } block is missing. Without it, Gradle does not use the JUnit Platform, so it finds no JUnit 5 tests and reports success anyway.
Exercise
Why does the Maven dependency use <scope>test</scope>?
Show answer
The test scope makes JUnit available only to code in src/test/java. It keeps JUnit out of the finished application that you ship.