Run JUnit 5 test cases in Maven project

Introduction:   This article demonstrates how to write JUnit 5 test cases and the process of running JUnit 5 test cases in Maven project.


For image download, domain name resolution and time synchronization, please click   Alibaba open source mirror station

1, Write JUnit 5 test cases

If you are a java developer, you should be no stranger to JUnit. JUnit is the basic tool for Java unit testing.
The latest version of JUnit is JUnit 5.x. in order to help Java developers use the latest version of JUnit, we will explain how to write JUnit 5 test cases.

1. Introduce 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 basis for launching the test framework on the JVM. It also defines the TestEngine API for developing test frameworks that run on the platform. In addition, the JUnit Platform also provides a console launcher (for starting the platform from the command line) and a JUnit 4-based runner for running 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 the JUnit Platform.
  • JUnit Jupiter is a combination of a new programming model and an extension model for writing tests and extensions in JUnit 5. Jupiter subproject provides a TestEngine for running Jupiter based tests on the platform.
  • JUnit Vintage provides a TestEngine 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 on demand. This provides multiple options for introducing JUnit 5 dependencies.
Generally speaking, if you want to save trouble, you can 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>

2. 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 12
 * @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 12
 * @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. I want to test whether the getWords method of HelloWorld is consistent with the expectation.
What needs to be emphasized here is the difference between JUnit 5 and JUnit 4:

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

2, Run JUnit 5 test cases

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

1. Run in IDE

Take the Eclipse IDE as an example, right-click a class or method and select "run as - > JUnit test". As shown in the figure below.
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-tONJQN3t-1587044607054)(../images/post/20200412-ide.jpg)]

2. Perform the test through Maven

The commands for executing test cases 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] ------------------------------------------------------------------------

The above results show that there are no failed use cases, but at the same time, you also find that there are no successful use cases. Because the test cases were not executed at all.
This is because JUnit 5 test cases cannot be directly identified in Maven. How to solve it? At this time, you need to add two additional plug-ins Maven Surefire or Maven Failsafe.

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

If you execute the test case again in Maven, 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 tested and executed.

  This article is transferred from: Run JUnit 5 test case in Maven project - Alibaba cloud developer community

Keywords: node.js Front-end Alibaba Cloud

Added by Weirdan on Tue, 30 Nov 2021 20:56:25 +0200