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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public final class StringUtils { public static String concat(String... sts) { String retVal = null; if (sts != null && sts.length > 0) { StringBuilder sb = new StringBuilder(); for (String st : sts) { if (st != null) { sb.append(st); } } retVal = sb.toString(); } return retVal; } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class StringUtilsTestUnit5Disabled { @Test public void testConcatWithRegularInput() { String st1 = "Hello"; String st2 = "World"; String st3 = "!"; String expect = st1 + st2 + st3; String actual = StringUtils.concat(st1, st2, st3); assertEquals(expect, actual); } @Test public void testConcatWithNullInput() { String st1 = "Hello"; String st2 = "World"; String st3 = null; String expect = st1 + st2; String actual = StringUtils.concat(st1, st2, st3); assertEquals(expect, actual); } @Test public void testConcatWithAllNullInput() { String actual = StringUtils.concat(); assertNull(actual); } } |
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.
1 2 3 4 5 6 7 8 9 10 |
@Disabled @Test public void testConcatWithNullInput() { String st1 = "Hello"; String st2 = "World"; String st3 = null; String expect = st1 + st2; String actual = StringUtils.concat(st1, st2, st3); assertEquals(expect, actual); } |
Now let’s see the output test results when we run the test class.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @Disabled public class StringUtilsTestUnit5Disabled { @Test public void testConcatWithRegularInput() { String st1 = "Hello"; String st2 = "World"; String st3 = "!"; String expect = st1 + st2 + st3; String actual = StringUtils.concat(st1, st2, st3); assertEquals(expect, actual); } @Test public void testConcatWithNullInput() { String st1 = "Hello"; String st2 = "World"; String st3 = null; String expect = st1 + st2; String actual = StringUtils.concat(st1, st2, st3); assertEquals(expect, actual); } @Test public void testConcatWithAllNullInput() { String actual = StringUtils.concat(); assertNull(actual); } } |
All the test methods in this class will be disabled during the test runner runs. Let’s see the output result on my Eclipse.
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 Test Suite – Aggregating Tests In Suites
JUnit 5 Dynamic Tests – Generate Tests at Run-time
JUnit 5 Assumptions With Assume
There’s a typo in 2.2. You wrote “@Diabled” instead of “@Disabled”.
Fixed that. Thank you very much.