Introduction
Sometimes you want to skip a test temporarily without deleting it. @Disabled helps you do that easily.
Sometimes you want to skip a test temporarily without deleting it. @Disabled helps you do that easily.
@Disabled("optional reason")
void testMethod() {
// test code
}You can add an optional reason inside the parentheses to explain why the test is disabled.
This annotation works on test methods or whole test classes.
@Disabled
@Test
void testFeature() {
// test code
}@Disabled("Waiting for bug fix #123")
@Test
void testLogin() {
// test code
}@Disabled
class OldTests {
@Test
void oldTest() {
// test code
}
}This class has two tests. The subtractTest is skipped because of @Disabled. The addTest runs and passes.
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test void addTest() { assertEquals(5, 2 + 3); } @Disabled("Skipping subtraction test temporarily") @Test void subtractTest() { assertEquals(1, 3 - 2); } }
Disabled tests do not run and do not affect test results.
Use @Disabled to keep your test code but skip running it temporarily.
@Disabled skips tests without deleting them.
You can add a reason to explain why the test is skipped.
It works on both test methods and classes.
@Disabled annotation in JUnit?@Disabled@Disabled annotation tells JUnit to skip running the test method or class.@Disabled with an optional string reason in parentheses and quotes.@Disabled("Waiting for bug fix")
public class CalculatorTests {
@Test
public void testAdd() { /* test code */ }
@Test
public void testSubtract() { /* test code */ }
}
What will happen when this test class runs?@Disabled is on a class, all tests inside are skipped.testAdd and testSubtract will be skipped, and the reason "Waiting for bug fix" will be shown in reports.@Disabled("Feature incomplete")
public void testFeature() {
assertEquals(5, 2 + 3);
}
But when you run tests, this method still runs. What is the likely problem?@Test annotation to recognize a method as a test.@Disabled but lacks @Test, so JUnit ignores it as a test and does not skip it.@Disabled("Reason") on test methods skips them and shows the reason in reports.@Test hides tests but does not show skip reasons. Disabling whole class skips all tests without selective reasons.Pass the test and this pattern moves unknown