JUnit 5 - How to disable or ignore tests
In JUnit 4, you could apply the @Ignore
annotation to a test method to skip its execution.
This annotation is no longer available in JUnit 5, you should use the @Disabled
annotation instead.
The JUnit 5 documentation
covers test disabling extensively, the following sections showcase the usage of the @Disabled
annotation.
@Disabled annotation applied to a class
You can apply the @Disabled
annotation to a class to disable all the tests in that class (suite).
@Disabled
class DisabledClassTest {
@Test
void test() {
fail("This test should not be executed");
}
}
@Disabled annotation applied to a method
You can apply the @Disabled
annotation to a method to disable that specific test.
class DisabledMethodTest {
@Disabled
@Test
void test() {
fail("This test should not be executed");
}
}
In addition to the test being disabled, none of the method-lifecycle callbacks such as @BeforeEach
or @AfterEach
will be invoked for that test.
Providing a reason for disabling a test
One of my favorite features of the @Disabled
annotation as compared to JUnit 4 @Ignore
annotation
is the ability to provide a reason for disabling a test.
@Disabled("This test is disabled because of a bug in the system BUG-1337")
class DisabledClassTest {
@Test
void test() {
fail("This test should not be executed");
}
}