To continue the series of the JUnit 5 tutorials, I’d like to share how to disable or ignore a test in JUnit 5. During the development, we may want to temporarily ignore or disable a test, a group of tests or even disable a test class for some reasons. There are several ways for us to do that. We can either comment out a test method or delete the @Test annotation on that method. However, the test runners will not report such a test on the test results. This may lead us to miss some test cases because of forgetting to fix them. Fortunately, JUnit 5 provides us an annotation to disable or ignore a test but still include it in the test report, and it is @Disabled. Notice that with JUnit 4, we can use @Ignore annotation to achieve the same purpose

1. Preparation

The source code for this example can be found on Github.

We will need to get your development environment ready with JUnit 5. To do that, please refer to another tutorial, which mentions using JUnit 5 with Eclipse, Maven, and Gradle.

JUnit 5 Basic Introduction

2. Disable or Ignore A Test In JUnit 5

Assume that we have a class StringUtils, which includes some utility methods to deal with strings in Java, has a concat method as below:

The method simply receives a list of strings and join them into a single string.

Now we will try to write some several tests for this method.

2.1. Some test cases for the concat method

Let’s see some test cases prepared for the method as follows:

2.2. Disable or Ignore a test

Let’s assume that we want to disable the test: testConcatWithNullInput. With JUnit 5, we simply need to put the annotation @Disabled on the method signature.

Now let’s see the output test results when we run the test class.

JUnit 5 Disable or Ignore A Test

JUnit 5 Disable or Ignore A Test

We can see that the test runner reports that there were 3 test cases run,  but 1 was skipped, and the test which was marked @Disabled has a different icon with the other.

2.3. Disable or Ignore a Test Class

If we want to disable a test class, we simply need to put the @Disabled annotation on the top of that class’s name. For example:

All the test methods in this class will be disabled during the test runner runs. Let’s see the output result on my Eclipse.

JUnit 5 Disable or Ignore A Test Class

JUnit 5 Disable or Ignore A Test Class

Note that the test report shows that there were 3 test cases disabled or ignored during the test runner ran.

4. Conclusion

We’ve learned how to disable or ignore a test method, a group of tests, or a test class by using JUnit 5 @Disabled annotation. When you use that feature, remember to back to all the disabled tests to fix them later.

In next posts, I’d like to share more about JUnit 5 features like JUnit 5 assumption test, JUnit 5 annotations. Recently, I had some posts related to JUnit 5. If you’re interested in them, you can refer to following links:

JUnit 5 Tutorial

JUnit 5 Basic Introduction

JUnit 5 vs JUnit 4

JUnit 5 Assertions Example

JUnit 5 Annotations Example

JUnit 5 Test Suite – Aggregating Tests In Suites

JUnit 5 Exception Testing

JUnit 5 Dynamic Tests – Generate Tests at Run-time

JUnit 5 Nested Tests Examples

JUnit 5 Assumptions With Assume

JUnit 5 Maven Example

JUnit 5 with Gradle Example

 

0 0 vote
Article Rating