Add JUnit 5 to Maven Project | Java Fresher
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
There are really two pieces to getting JUnit 5 running in Maven: the dependency and the plugin. For the dependency, the easiest thing is the junit-jupiter aggregator artifact, because it pulls in everything I need — the API so my tests compile, the engine so they actually run, and the params module for parameterized tests. I put it in test scope so it never ends up in the production jar.
The second piece is the Maven Surefire plugin, which is what runs unit tests during mvn test. It must support the JUnit Platform; native support starts at 2.22.0, while current projects normally use a 3.x release, and if you forget that, mvn test happily reports zero tests run, which is confusing.
Now, in practice, if I'm on a Spring Boot project I almost never do any of that, because spring-boot-starter-test already bundles JUnit 5, Mockito, AssertJ, and Hamcrest, all in test scope, with compatible versions managed for me. So I just add that one starter and I'm done.
Common mistakes are adding only the junit-jupiter-api artifact, which makes tests compile but not run because the engine is missing, using an old Surefire that silently ignores the tests, and forgetting the test scope.
Add JUnit 5 to Maven Project | Java Fresher
Direct Answer
Add JUnit 5 to Maven with the junit-jupiter aggregator dependency in test scope, and make sure the Maven Surefire plugin is recent enough to support the JUnit Platform (2.22.0 or newer; current projects normally use 3.x) to discover Jupiter tests. In a Spring Boot project you get all of this for free via spring-boot-starter-test, so you rarely add JUnit directly.
Code Example
<!-- Plain Maven project -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId> <!-- api + engine + params -->
<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> <!-- 3.x discovers JUnit Platform tests -->
</plugin>
</plugins>
</build>
<!-- Spring Boot: this single starter already includes JUnit 5 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Common Mistakes & Pitfalls
- Only adding
junit-jupiter-api. Tests compile but fail to run because the engine isn't on the path. Use thejunit-jupiteraggregator. - Old Surefire. Surefire versions older than 2.22.0 do not natively discover JUnit Platform tests, so
mvn testreports zero tests run. - Wrong scope. Forgetting
<scope>test</scope>leaks the test framework into the production jar. - Adding JUnit on top of Spring Boot. Pulling a conflicting JUnit version can clash with the version the starter already manages.
Check your understanding
Answer these without looking back at the article.
- How do you run only a single test class with Maven?
- What is the difference between mvn test and mvn verify?
- What does spring-boot-starter-test include?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.