Running JUnit 5 test cases in Maven project

Keywords: Programming Junit Maven Java Eclipse

This article demonstrated how to write JUnit 5 test cases and run JUnit 5 test cases in Maven project.

Write JUnit 5 test cases

If you're a java developer, JUnit should be familiar. JUnit is the basic tool for Java unit testing.

At present, the latest version of JUnit is JUnit 5.x, but most Java developers are still in JUnit 4.x, so it is necessary to demonstrate how to write JUnit 5 test cases.

Introducing JUnit 5 dependency

Compared with JUnit 4, a big change of JUnit 5 is that JUnit 5 has a new API different from JUnit 4. JUnit 5 is divided into three parts:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
  • The JUnit Platform is the foundation for launching the test framework on the JVM. It also defines the test engine API, which is used to develop the test framework running on the platform. In addition, the JUnit Platform provides a console launcher to launch the platform from the command line and a JUnit 4-based runner to run any TestEngine on the platform in a JUnit 4-based environment. Popular ides (IntelliJ IDEA, Eclipse, NetBeans, Visual Studio Code, etc.) and build tools (Gradle, Maven, Ant, etc.) also have first-class support for JUnit Platform.
  • JUnit Jupiter is a combination of a new programming model and an extension model for writing tests and extensions in JUnit 5. The Jupiter subproject provides a test engine for running Jupiter based tests on the platform.
  • JUnit Vintage provides a test engine for running JUnit 3-based and JUnit 4-based tests on the platform.

Therefore, in Maven, JUnit is divided into 5 modules, which means that you can introduce any module defined above as required. This gives you multiple options for introducing JUnit 5 dependencies.

Generally speaking, it is easy to introduce JUnit Jupiter dependency. JUnit Jupiter is the aggregation package of commonly used JUnit 5 modules.

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>${junit-jupiter.version}</version>
    <scope>test</scope>
</dependency>

Write test cases

Here is a simple Java program:

/**
 * Welcome to https://waylau.com
 */
package com.waylau.java.demo;

/**
 * Hello World.
 * 
 * @since 1.0.0 2020 April 12th 2013
 * @author <a href="https://waylau.com">Way Lau</a>
 */
public class HelloWorld {

	private String words;

	public HelloWorld(String words) {
		this.words = words;
	}
	
	public String getWords() {
		return words;
	}

}

According to the management, we will create a corresponding unit test case in the test directory of Maven project:

/**
 * Welcome to https://waylau.com
 */
package com.waylau.java.demo;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * HelloWorld Test.
 * 
 * @since 1.0.0 2020 April 12th 2013
 * @author <a href="https://waylau.com">Way Lau</a>
 */
class HelloWorldTests {

	@Test
	void testGetWords() {
		var words = "Hello World";
		var hello = new HelloWorld(words);
		
		assertEquals(words, hello.getWords());
	}
}

The above use case is very simple, which is to test whether the getWords method of HelloWorld is consistent with the expected. What needs to be emphasized here is the difference between JUnit 5 and JUnit 4:

  • The API used by JUnit 5 is under the package org.junit.jupiter.api. *
  • Test methods (such as testGetWords in the above example) can be used without adding public.

Running JUnit 5 test cases

As mentioned above, JUnit 5 is supported in most mainstream ides. So you can choose to run it in the IDE, or you can run tests through Maven.

Run in IDE

Take the Eclipse IDE as an example. Right click the class or method and select run as - > JUnit test. As shown in the figure below.

[external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-tONJQN3t-1587044607054)(../images/post/20200412-ide.jpg))

Test through Maven

The commands to execute the test case in Maven are as follows:

mvn test

If you execute the above command, you will get the following test results

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.waylau.java.demo.HelloWorldTests
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.983 s
[INFO] Finished at: 2020-04-12T11:22:16+08:00
[INFO] ------------------------------------------------------------------------

There are no failed use cases in the above results, but at the same time, you also find that there are no successful use cases. Because there are no test cases executed at all.

This is because JUnit 5 test cases are not directly recognized in Maven. How to solve it? At this time, two additional plug-ins, Maven Surefire or Maven Failsafe, are needed.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven-failsafe-plugin.version}</version>
        </plugin>
    </plugins>
</build>

Execute the test case again in Maven, and you will get the following test results:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.waylau.java.demo.HelloWorldTests
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.045 s - in com.waylau.java.demo.HelloWorldTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.116 s
[INFO] Finished at: 2020-04-12T11:30:29+08:00
[INFO] ------------------------------------------------------------------------

As you can see, the HelloWorldTests class has been executed.

Reference reference

Posted by xylex on Thu, 16 Apr 2020 09:26:50 -0700