Unit test is very essential in ensuring the quality of source code developed by developers. In the Java world, JUnit is a very simple framework that supports developers to implement Unit tests of their source code.  The current stable version of JUnit is 4.12. However, with the goal is to support new features in Java 8 and above, as well as enabling many different styles of testing, the JUnit team is about to release the new version of JUnit, JUnit 5. In this post, I’d like to introduce about JUnit 5, its new features, and some examples.

JUnit 5 basic

JUnit 5

1. What’s New in JUnit 5?

JUnit 5 is composed of several different modules from three different sub-projects.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

1.1. JUnit Platform

  • Be responsible for launching testing frameworks on the JVM. It defines a stable and powerful interface between JUnit and its clients such as build tools (Maven, Gradle, Ant, etc) and IDEs (Eclipse, IntelliJ, NetBeans, etc). The final objective is how its clients get integrated easily with JUnit in discovering and executing the tests.
  • Defines the TestEngine API for developing a testing framework that runs on the Unit platform. So, 3rd testing libraries like Spock, Cucumber, and FitNesse can be plugged into JUnit platform by implementing a custom TestEngine.
  • Provides a ConsoleLauncher to launch the platform from the command line and build plugins for Gradle, Maven, and JUnit 4

1.2. JUnit Jupiter

This module includes new programming model and extension model for writing tests and extensions in JUnit 5. We will see many new annotations like @BeforeEach, @AfterEach, @DisplayName, @Nested, @Tag, etc.

1.3. JUnit Vintage

Supports running JUnit 3 and JUnit 4 based tests on the platform.

2. Supported Java Versions

JUnit 5 requires Java 8. However, we can still test code that has been compiled with previous versions of the JDK.

3. JUnit 5 Annotations vs JUnit 4.x Annotation

JUnit 5 Annotations Descriptions JUnit 4 Annotations
@Test Declares a test method @Test
@TestFactory Denotes a method is a test factory for dynamic tests in JUnit 5 N/A
@DisplayName Define custom display name for a test class or test method N/A
@BeforeEach Denotes that the annotated method will be executed before each test method(annotated with @Test) in the current class. @Before
@AfterEach Denotes that the annotated method will be executed after each test method (annotated with @Test) in the current class. @After
@BeforeAll Denotes that the annotated method will be executed before all test methods in the current class. @BeforeClass
@AfterAll Denotes that the annotated method will be executed after all test methods in the current class. @AfterClass
@Nested Denotes that the annotated class is a nested, non-static test class N/A
@Tag Declare tags for filtering tests. N/A
@Disable Is used to disable a test class or method. @Ignore
@ExtendWith Is used to register custom extensions in JUnit 5 N/A
@RepeatedTest Is used to repeat tests N/A

To compare JUnit 5 and JUnit 4, please visit JUnit 5 vs JUnit 4

4. JUnit 5 Basic Examples

4.1. Sample Source Code and Build file

You can get JUnit 5 basic examples with Maven and Gradle build files at this repository on Github.

4.2. JUnit 5 Basic Example with Maven

Here is an example pom.xml file.

To completely setup a JUnit 5 Maven project, please visit JUnit 5 Maven Example.

4.3. JUnit 5 Basic Example with Gradle

Here is an example build.gradle file.

To completely setup a JUnit 5 Gradle project, please visit JUnit 5 Gradle Example.

4.4. JUnit 5 Basic Example Tests

Following is a JUnit 5 example test class. Assume that we have a class: UserRepositoryImplFile that can be used to store user information in an XML file, and now we’re writing Unit tests for that class.

5. Run JUnit 5 Tests

5.1. Run JUnit 5 tests with Maven.

To run JUnit 5 tests with Maven, we simply need to go to project directory and issue below command:

The output for above test class is similar to below:

5.2. Run JUnit 5 test with Gradle

In similar to above, run JUnit 5 test with Gradle is very simple.

The output of above test class is similar to below:

5.3. Run JUnit 5 test with IntelliJ

IntelliJ supports JUnit 5 by default. Therefore, running JUnit 5 on IntelliJ is pretty simple, simply Right click  –> Run, or Ctrl-Shift-F10

JUnit 5 Basic - Run JUnit 5 with Intellij

Run JUnit 5 with IntelliJ

5.4. Run JUnit 5 test with Eclipse

Currently, there is no direct support to run Unit tests on the JUnit Platform within IDEs. This means that, in Eclipse, when you right click on the test class, select Run As on the menu,  there will be no JUnit Test menu item for you to select. However, we can work around by running the JUnit 5 tests with a JUnit 4 based Runner. In short, we can annotate our class with the @RunWith(JUnitPlatform.class) annotation. Let’s take a look again at our above test class.

Now the IDE will recognize the JUnit 5 test classes and allow us to run the tests directly in the IDE. In Eclipse, we just need to open the test class, right click on the class, under Run As menu, select JUnit Test menu item and the test class will be executed.

Here is the result in my Eclipse.

JUnit 5 Tutorial - Run JUnit 5 With Eclipse

JUnit 5 Tutorial – Run JUnit 5 With Eclipse

6. Conclusion

We have just taken a quick look at JUnit 5 which includes:

  • Some new features of JUnit5
  • Some new annotations of JUnit 5
  • Some JUnit 5 basic examples with Maven and Gradle
  • Run JUnit 5 tests with Maven, Gradle, Eclipse, and IntelliJ IDEA.

Hopefully, they’re enough for you to get started with JUnit 5. If you want more JUnit 5 tutorials, you can refer to my recent posts:

JUnit 5 Tutorial

JUnit 5 Annotations Example

JUnit 5 Assertions Example

JUnit 5 and Spring Boot Example

JUnit 5 Disable or Ignore A Test

JUnit 5 Exception Testing

JUnit 5 Dynamic Tests – Generate Tests at Run-time

JUnit 5 Nested Tests Examples

JUnit 5 Test Suite – Aggregating Tests In Suites

JUnit 5 Maven Example

JUnit 5 with Gradle Example

JUnit 5 Parameter Resolution Example

 

 

 

0 0 vote
Article Rating