Report this

What is the reason for this report?

JUnit Setup Maven - JUnit 4 and JUnit 5

Published on August 3, 2022
JUnit Setup Maven - JUnit 4 and JUnit 5

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?

With Junit 4, it’s working perfectly. Thanks

- arun

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”.

- Dante

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.