0
0
JUnittesting~15 mins

@Disabled for skipping tests in JUnit - Build an Automation Script

Choose your learning style9 modes available
Skip a test method using @Disabled annotation
Preconditions (2)
Step 1: Annotate one test method with @Disabled
Step 2: Run the test class
Step 3: Observe that the disabled test method is skipped
Step 4: Verify that other test methods run normally
✅ Expected Result: The test method annotated with @Disabled is skipped and not executed. Other test methods run and pass or fail as usual.
Automation Requirements - JUnit 5
Assertions Needed:
Verify that the disabled test method is not executed
Verify that other test methods execute normally
Best Practices:
Use @Disabled annotation directly above the test method
Keep test methods independent
Use descriptive names for disabled tests explaining why they are skipped
Automated Solution
JUnit
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DisabledTestExample {

    @Test
    void activeTest() {
        assertTrue(5 > 2, "Active test should run and pass");
    }

    @Disabled("Skipping this test temporarily due to bug #123")
    @Test
    void skippedTest() {
        // This test will be skipped
        assertTrue(false, "This test should be skipped and not fail");
    }
}

The code defines two test methods inside a test class.

The activeTest method is a normal test and will run when the tests are executed.

The skippedTest method is annotated with @Disabled and a reason string. This tells JUnit to skip this test during execution.

Assertions inside activeTest verify normal behavior. The assertion inside skippedTest would fail if run, but since the test is disabled, it is not executed, so no failure occurs.

This setup helps temporarily skip tests without deleting or commenting them out.

Common Mistakes - 3 Pitfalls
Using @Disabled without a reason string
Disabling tests permanently without tracking
Using @Disabled on the whole test class without need
Bonus Challenge

Now add a third test method that is conditionally disabled only on Windows OS

Show Hint