In this tutorial, we will get to know about JUnit 5 Nested Tests, how to use the JUnit 5 @Nested annotation to express the relationship among several group of tests.

Let’s say we need to write tests for a class that has some functions and several of them have complex business domain logic. Normally, we can put on the tests for all methods of that class in the same test class. However, if we want to group all the test methods for those several complex methods together, or if we want to group all related test methods by features, we can use the JUnit 5 @Nested annotation to achieve those purposes.

Let’s try some examples about JUnit 5 Nested Tests below.

1. Preparation

The only one prerequisite for this tutorial is getting JUnit 5 be ready on your environment. You can refer to my recently post for getting JUnit 5 setup with Eclipse and build tools like Maven and Gradle and other JUnit 5 tutorials as well.

The sample source code for this post is on Github. You may want to take a look at it.

2. JUnit 5 Nested Tests

To demo for the JUnit 5 nested tests feature, assume that we have an UserService.java class which has 4 methods: login, logout, changePassword and resetPassword as below.

We also assume that 2 methods: login and changePassword are complex and we want to group all the test methods of each into different groups by using the JUnit 5 @Nested annotation.

Let’s see the test class for above class.

Below are some explanations.

2.1. Import new packages of JUnit Jupiter

We have to import classes from JUnit 5. All are stared with: org.junit.jupiter.api

2.2. To run the tests on Eclipse (Right click –> Run As –> JUnit Tests)

Temporarily, we need to add the JUnit 4 annotation: @RunWith(JUnitPlatform.class)

2.3. Group all test methods related to Login feature in an inner class, and annotate the class with @Nested

We create an inner class, annotate it with the @Nested annotation and put all tests methods related to the Login feature in this inner class.

2.4. Group all test methods related to ChangePassword feature into an inner class, and annotate the class with @Nested

2.5. All the tests for remaining methods, we still put in the outer class.

For example:

3. Run the test class and result.

To run the test on Eclipse, simply Right Click –> Run As –> JUnit Tests.

As for running with Maven and Gradle, you can refer to this post: JUnit 5 Basic Introduction

Here is the output on my Eclipse.

JUnit 5 Nested Tests

JUnit 5 Nested Tests

We can see that there are 2 groups for 2 features we defined above.

4. Conclusions.

We have learned about the JUnit 5 Nested Tests feature which allows developer to group all related tests in the same tests class together by using inner class and JUnit 5 @Nested annotation.

In future posts, we’ll continue to dive into other features of JUnit 5. Recently, I have some posts related to the JUnit 5. If you’re interested in, you can find them in the following links:

JUnit 5 Tutorial

JUnit 5 Basic Introduction

JUnit 5 vs JUnit 4

JUnit 5 Annotations Example

JUnit 5 Assertions Example

JUnit 5 Disable or Ignore A Test

JUnit 5 Exception Testing

JUnit 5 Test Suite – Aggregating Tests In Suites

JUnit 5 Assumptions With Assume

JUnit 5 Maven Example

JUnit 5 with Gradle Example

JUnit 5 Parameter Resolution Example

Timeout Test in JUnit 5

 

0 0 vote
Article Rating