In JUnit 5, you can disable a test by annotating it with @Disabled from the org.junit.jupiter.api package.
Disable a Single Test Method
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class CalculatorTest {
@Test
@Disabled
void divisionByZeroTest() {
// This test will be skipped
}
}
When you run the test suite, this test will not be executed.
Add a Reason
It is a good practice to include a reason so other developers know why the test is disabled.
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class CalculatorTest {
@Test
@Disabled("Temporarily disabled until division-by-zero handling is fixed")
void divisionByZeroTest() {
// This test will be skipped
}
}
Disable an Entire Test Class
You can also place @Disabled on a test class to skip all tests inside it.
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@Disabled("Disabled while refactoring the calculator module")
class CalculatorTest {
@Test
void additionTest() {
// This test will be skipped
}
@Test
void subtractionTest() {
// This test will also be skipped
}
}
Summary
Use:
@Disabled
or preferably:
@Disabled("Reason why this test is disabled")
The required import is:
import org.junit.jupiter.api.Disabled;
@Disabled is useful for temporarily skipping tests that are incomplete, unstable, or depend on functionality that is not ready yet.
