What if you could skip broken tests instantly without messy code changes?
Why @Disabled for skipping tests in JUnit? - Purpose & Use Cases
Imagine you have a big set of tests for your app. Some tests are broken or not ready yet. You want to skip them temporarily while running others.
Without a simple way to skip, you have to comment out code or remove tests manually.
Manually commenting out tests is slow and error-prone. You might forget to uncomment later, or accidentally skip important tests.
This wastes time and can cause confusion in your team.
The @Disabled annotation lets you easily skip tests without deleting or commenting them out.
You just add @Disabled above a test, and JUnit will ignore it during runs.
// Commenting out test
// @Test
// public void testFeature() { ... }@Disabled
@Test
public void testFeature() { ... }You can quickly skip tests to focus on what matters, keeping your code clean and your test runs efficient.
When a feature is under development and its test is failing, you can @Disabled that test temporarily. This keeps your build green while you fix the feature.
Manually skipping tests is slow and risky.
@Disabled provides a clean, safe way to skip tests.
It helps keep test runs focused and reliable.
Practice
@Disabled annotation in JUnit?Solution
Step 1: Understand the role of
The@Disabled@Disabledannotation tells JUnit to skip running the test method or class.Step 2: Compare with other options
It does not mark tests as passed, run them multiple times, or affect speed.Final Answer:
To skip a test without deleting its code -> Option BQuick Check:
@Disabled skips tests = C [OK]
- Thinking @Disabled runs tests faster
- Confusing @Disabled with @Test
- Assuming @Disabled marks tests as passed
Solution
Step 1: Identify correct annotation and syntax
JUnit uses@Disabledwith an optional string reason in parentheses and quotes.Step 2: Check each option
@Disabled("Not ready yet")\npublic void testFeature() {} uses correct annotation and syntax. Options A and C use wrong annotations. @Disabled public void testFeature() {"Not ready yet"} places the reason incorrectly.Final Answer:
@Disabled("Not ready yet")\npublic void testFeature() {} -> Option DQuick Check:
@Disabled("reason") syntax = B [OK]
- Using @Skip or @Ignore instead of @Disabled
- Placing reason outside parentheses
- Missing quotes around reason
@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?Solution
Step 1: Understand class-level @Disabled effect
When@Disabledis on a class, all tests inside are skipped.Step 2: Check test methods behavior
BothtestAddandtestSubtractwill be skipped, and the reason "Waiting for bug fix" will be shown in reports.Final Answer:
Both testAdd and testSubtract will be skipped with reason shown -> Option AQuick Check:
Class-level @Disabled skips all tests = D [OK]
- Thinking only first test is skipped
- Assuming tests run despite class @Disabled
- Confusing @Disabled with @Test
@Disabled("Feature incomplete")
public void testFeature() {
assertEquals(5, 2 + 3);
}
But when you run tests, this method still runs. What is the likely problem?Solution
Step 1: Check required annotations for test methods
JUnit requires@Testannotation to recognize a method as a test.Step 2: Analyze given code
The method has@Disabledbut lacks@Test, so JUnit ignores it as a test and does not skip it.Final Answer:
The method is missing the @Test annotation -> Option AQuick Check:
Missing @Test means method not recognized as test [OK]
- Assuming @Disabled alone makes method a test
- Ignoring method visibility rules
- Thinking JUnit does not support @Disabled on methods
Solution
Step 1: Understand how to skip tests with reasons
Using@Disabled("Reason")on test methods skips them and shows the reason in reports.Step 2: Compare other options
Commenting out or removing@Testhides tests but does not show skip reasons. Disabling whole class skips all tests without selective reasons.Final Answer:
Add @Disabled("Reason") to each test method you want to skip -> Option CQuick Check:
@Disabled with reason per method shows skip reason [OK]
- Commenting out tests instead of disabling
- Removing @Test hides tests silently
- Disabling whole class loses selective control
Ready to prove it?
Pass the test and this pattern moves unknown
