Introduction
This post explains how to disable or ignore a unit test and an entire test class. The annotations differ between JUnit 5 and JUnit 4, so I will cover both annotations in this post.
Disable (skip) unit tests in JUnit 5 /
To disable a test or test class in JUnit 5 you use the @Disabled
annotation.
In the following example, I use the @Disabled
annotation to disable a single unit test in JUnit 5.
|
|
The example shows how you can ignore a test with JUnit 5.
Disable a test class
To ignore all the tests inside a class, you place the @Disabled
annotation at the class level. In the following example
we placed the annotation above the class name. Doing so will disable all the unit tests inside the class.
|
|
Ignore unit tests in JUnit 4
To ignore a unit tests in JUnit 4 you use the @Ignore
annotation.
In the following example, I use the @Ignore
annotation to disable a single unit test in JUnit 4.
|
|
Ignore a test class
To ignore all the unit tests inside a class, you have to place the @Ignore
annotation at the class level.
In the following example the @Ignore
is above the class, so none of the unit tests inside the class will be executed.
|
|
Conclusion
In this post, we looked at how to disable and ignore unit tests using JUnit 5 and JUnit 4. You learned how skip a test with JUnit using both JUnit 5 and 4. Using @Ignore or @Disabled will make Junit skip the test.
Further reading
More about testing in Java: