Test Overview
This test demonstrates how to use the @Disabled annotation in JUnit to skip a test method. The test verifies that a simple addition operation works correctly, but the test is skipped during execution.
This test demonstrates how to use the @Disabled annotation in JUnit to skip a test method. The test verifies that a simple addition operation works correctly, but the test is skipped during execution.
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class CalculatorTest { @Test @Disabled("Skipping this test temporarily") void testAddition() { int result = 2 + 3; assertEquals(5, result, "2 + 3 should equal 5"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner discovers testAddition method annotated with @Test and @Disabled | Test suite loaded with one test method marked as disabled | — | PASS |
| 2 | Test runner skips execution of testAddition due to @Disabled annotation | No browser or UI involved; test framework skips test execution | No assertions executed because test is skipped | PASS |
| 3 | Test report marks testAddition as skipped | Test report shows one skipped test | Skipped test is correctly reported | PASS |
@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