Tutorial

JUnit Setup Maven - JUnit 4 and JUnit 5

Published on August 3, 2022
Default avatar

By Pankaj

JUnit Setup Maven - JUnit 4 and JUnit 5

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.

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 us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 10, 2020

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

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 24, 2019

    With Junit 4, it’s working perfectly. Thanks

    - arun

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel