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 we will cover both annotations in this post.
Disable (ignore) unit tests in JUnit 5
To disable a unit test or class in JUnit 5 you use the @Disabled
annotation.
Disable a single test
In the following example, we use the @Disabled
annotation to disable a single unit test in 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 (disable) unit tests in JUnit 4
To ignore a unit tests in JUnit 4 you use the @Ignore
annotation.
Ignore a single test
In the following example, we 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
Further reading
More about testing in Java: