JUnit 4 and JUnit 5 in Maven Projects

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.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

centron Managed Cloud Hosting in Deutschland

JSP Exception Handling – Tutorial

Apache
JSP Exception Handling – Tutorial To handle exceptions thrown by the JSP page, all we need is an error page and define the error page in JSP using jsp page…
centron Managed Cloud Hosting in Deutschland

JUnit HTML Report – Tutorial

Apache
JUnit HTML Report – Tutorial When we configure maven-surefire-plugin to run our JUnit tests, it generates surefire-reports directory. This directory contains a txt file and an XML file for every…