JUnit 4 and JUnit 5 are completely different frameworks. They both serve the same purpose, but the JUnit 5 is a completely different testing framework written from scratch. It’s not using anything from JUnit 4 APIs.
Here we will look into how to setup JUnit 4 and JUnit 5 in our maven projects.
JUnit Maven Dependencies
If you want to use JUnit 4, then you need a single dependency as below.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
JUnit 5 is divided into several modules, you need at least JUnit Platform and JUnit Jupiter to write tests in JUnit 5. Also, note that JUnit 5 requires Java 8 or higher versions.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
If you want to run parameterized tests, then you need to add an additional dependency.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
JUnit Tests During Maven Build
If you want the tests to be executed during the maven build, you will have to configure maven-surefire-plugin
plugin in your pom.xml file.
JUnit 4:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
<configuration>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
JUnit 5:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>src/test/java/</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
JUnit HTML Reports
Maven surefire plugin generates text and XML reports, we can generate HTML based reports using maven-surefire-report-plugin
. Below configuration works for both JUnit 4 and JUnit 5.
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</reporting>
Just run mvn site
command and the HTML report will be generated in the target/site/
directory.
That’s all for a quick roundup of JUnit setup for maven projects.
I’m having the following problem: When I run the unit test in IntelliJ some test fail, but using “mvn test” it says “Build Success”.
it worked! Thanks!
If it works via the Command line, then it’s correct. Please look at the error to understand why it failed in IntelliJ. Sometimes, the IDE environment is different from the system causing the issue.
With Junit 4, it’s working perfectly. Thanks