What is the purpose of the @Disabled annotation in JUnit?
The <code>@Disabled</code> annotation is used to skip or ignore a test method or class temporarily. It prevents the test from running without deleting or commenting out the code.
Click to reveal answer
beginner
How do you disable a single test method using JUnit?
Add the @Disabled annotation directly above the test method. For example:
@Disabled @Test void testExample() { ... }
Click to reveal answer
intermediate
Can you disable an entire test class in JUnit? How?
Yes, by placing the <code>@Disabled</code> annotation above the class declaration, all tests inside that class will be skipped during execution.
Click to reveal answer
intermediate
What happens to the test report when tests are skipped using @Disabled?
Skipped tests are reported as 'skipped' or 'ignored' in the test report. They do not count as passed or failed but are noted so you know they were intentionally not run.
Click to reveal answer
beginner
Why might you want to use @Disabled instead of deleting a test?
Using @Disabled keeps the test code for future use or debugging. It allows you to temporarily skip tests that are flaky, incomplete, or dependent on external factors without losing the test logic.
Click to reveal answer
What does the @Disabled annotation do in JUnit?
ARuns the test multiple times
BMarks the test as failed
CEnables the test to run only on weekends
DSkips the annotated test method or class
✗ Incorrect
The @Disabled annotation tells JUnit to skip the test method or class during execution.
Where do you place the @Disabled annotation to skip a test method?
AIn the test class constructor
BInside the test method body
CAbove the test method declaration
DBelow the test method declaration
✗ Incorrect
You place @Disabled directly above the test method declaration to skip that specific test.
If you want to skip all tests in a class, what should you do?
AAdd <code>@Disabled</code> above the class declaration
BAdd <code>@Disabled</code> to each test method
CDelete the test class
DRename the test class
✗ Incorrect
Adding @Disabled above the class disables all tests inside that class.
How are skipped tests shown in the test report?
AAs failed tests
BAs skipped or ignored tests
CAs passed tests
DThey are not shown at all
✗ Incorrect
Skipped tests appear in the report as skipped or ignored, so you know they were intentionally not run.
Why is it better to use @Disabled instead of deleting a test?
ATo keep the test code for future use
BTo make the test run faster
CTo automatically fix test errors
DTo run the test on a different machine
✗ Incorrect
@Disabled keeps the test code intact so you can enable it later or debug it.
Explain how and why you would use the @Disabled annotation in JUnit testing.
Think about situations when you want to skip tests without deleting them.
You got /4 concepts.
Describe the impact of using @Disabled on test reports and test suite results.
Consider how test reports show tests that were not executed.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the @Disabled annotation in JUnit?
easy
A. To run a test multiple times
B. To skip a test without deleting its code
C. To mark a test as passed automatically
D. To make a test run faster
Solution
Step 1: Understand the role of @Disabled
The @Disabled annotation 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 B
Quick Check:
@Disabled skips tests = C [OK]
Hint: Remember: @Disabled means skip test, not delete it [OK]
Common Mistakes:
Thinking @Disabled runs tests faster
Confusing @Disabled with @Test
Assuming @Disabled marks tests as passed
2. Which of the following is the correct syntax to disable a test method with a reason in JUnit?
easy
A. @Skip("Not ready yet")\npublic void testFeature() {}
B. @Disabled public void testFeature() {"Not ready yet"}
C. @Ignore("Not ready yet")\npublic void testFeature() {}
D. @Disabled("Not ready yet")\npublic void testFeature() {}
Solution
Step 1: Identify correct annotation and syntax
JUnit uses @Disabled with 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 D
Quick Check:
@Disabled("reason") syntax = B [OK]
Hint: Use @Disabled("reason") with quotes and parentheses [OK]
Common Mistakes:
Using @Skip or @Ignore instead of @Disabled
Placing reason outside parentheses
Missing quotes around reason
3. Consider the following JUnit test class:
@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?
medium
A. Both testAdd and testSubtract will be skipped with reason shown
B. Only testAdd will be skipped, testSubtract will run
C. Both tests will run normally
D. Tests will fail due to syntax error
Solution
Step 1: Understand class-level @Disabled effect
When @Disabled is on a class, all tests inside are skipped.
Step 2: Check test methods behavior
Both testAdd and testSubtract will 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 A
Quick Check:
Class-level @Disabled skips all tests = D [OK]
Hint: Class @Disabled skips all tests inside it [OK]