Introduction
In this chapter, we will explore how to disable tests in JUnit. Disabling tests are useful when you want to temporarily prevent certain tests from running without deleting or modifying the test code. JUnit provides annotations to achieve this functionality.
What is Disabling Tests?
Disabling tests allows you to prevent specific test methods or classes from being executed during a test run. This can be useful in scenarios where a test is not ready, needs to be temporarily skipped due to a known issue, or when refactoring code.
The @Disabled annotation is used to disable a test method or a test class. When applied, JUnit will skip the execution of the annotated test or all tests within the annotated class.
Why Disable Tests?
There are several reasons why you might want to disable tests:
- Known Issues: If a test is failing due to a known bug or issue that you are working on, you might disable the test until the issue is resolved.
- Incomplete Features: If a test is for a feature that is not yet fully implemented, you might disable the test until the feature is complete.
- Refactoring: When refactoring code, you might temporarily disable tests that are affected by the changes until the refactoring is complete.
- External Dependencies: If a test relies on an external service or resource that is currently unavailable, you might disable the test until the dependency is back online.
- Flaky Tests: If a test is unreliable and fails intermittently, you might disable it while investigating the cause.
Disabling Tests Example using @Disabled Annotation
Let’s create examples to demonstrate how to disable tests using the @Disabled annotation.
Step 1: Create a Class Under Test
Calculator Class
The Calculator class will have basic arithmetic operations.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
Step 2: Create a Test Class
CalculatorTest
The CalculatorTest class will contain tests for the Calculator class. We will demonstrate how to disable individual test methods and entire test classes.
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CalculatorTest {
private Calculator calculator = new Calculator();
@Test
void testAdd() {
assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
}
@Disabled("Disabled until bug #42 is fixed")
@Test
void testSubtract() {
assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
}
@Test
void testMultiply() {
assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
}
@Disabled
@Test
void testDivide() {
assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
}
}
Step 3: Create a Disabled Test Class
You can also disable an entire test class. All test methods within a disabled class will be skipped.
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@Disabled("Disabled until the module is complete")
public class DisabledCalculatorTest {
private Calculator calculator = new Calculator();
@Test
void testAdd() {
assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
}
@Test
void testSubtract() {
assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
}
@Test
void testMultiply() {
assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
}
@Test
void testDivide() {
assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
}
}
Important Points
- Disabling Test Methods: The
@Disabledannotation can be used to disable specific test methods. This can be useful if a particular test is not ready to be run or if it is causing issues. - Disabling Test Classes: The
@Disabledannotation can also be used at the class level to disable all tests within the class. This is useful for larger refactorings or when a whole suite of tests needs to be temporarily ignored. - Optional Reason: The
@Disabledannotation accepts an optional string parameter that allows you to specify a reason for disabling the test. This can be helpful for documentation and for future reference.
Step 4: Running the Tests
To run the tests, simply run the CalculatorTest and DisabledCalculatorTest classes as JUnit tests. This will execute all enabled test methods while skipping the disabled ones.
Using IntelliJ IDEA
- Run Tests: Click the green run icon next to the
CalculatorTestorDisabledCalculatorTestclass and selectRun. - View Results: The results will be displayed in the Run window. Disabled tests will be marked as skipped.

Conclusion
JUnit provides a straightforward way to disable tests using the @Disabled annotation. Disabling tests can be useful in various scenarios, such as when dealing with known issues, incomplete features, ongoing refactoring, unavailable external dependencies, or flaky tests. By using the @Disabled annotation, you can temporarily skip specific tests or entire test classes without removing or modifying the test code, ensuring that your test suite remains manageable and understandable.