0
0
JUnittesting~5 mins

@Disabled for skipping tests in JUnit

Choose your learning style9 modes available
Introduction

Sometimes you want to skip a test temporarily without deleting it. @Disabled helps you do that easily.

When a test is broken and you want to fix it later without failing the build.
When a feature is not ready but the test exists already.
When you want to skip a long-running test during quick checks.
When a test depends on an external system that is currently unavailable.
Syntax
JUnit
@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.

Examples
Disables the test without giving a reason.
JUnit
@Disabled
@Test
void testFeature() {
    // test code
}
Disables the test and explains why.
JUnit
@Disabled("Waiting for bug fix #123")
@Test
void testLogin() {
    // test code
}
Disables all tests in the class.
JUnit
@Disabled
class OldTests {
    @Test
    void oldTest() {
        // test code
    }
}
Sample Program

This class has two tests. The subtractTest is skipped because of @Disabled. The addTest runs and passes.

JUnit
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);
    }
}
OutputSuccess
Important Notes

Disabled tests do not run and do not affect test results.

Use @Disabled to keep your test code but skip running it temporarily.

Summary

@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.