Bird
Raised Fist0
JUnittesting~20 mins

@Disabled for skipping tests in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit @Disabled Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of @Disabled Annotation in JUnit
What is the primary purpose of the @Disabled annotation in JUnit 5?
ATo run a test multiple times automatically
BTo mark a test as expected to fail
CTo enable parallel execution of tests
DTo temporarily skip the execution of a test method or class
Attempts:
2 left
💡 Hint
Think about why you might want to prevent a test from running temporarily.
❓ Predict Output
intermediate
2:00remaining
Effect of @Disabled on Test Execution Count
Given the following JUnit 5 test class, how many tests will be executed when running this class?
JUnit
import org.junit.jupiter.api.*;

public class SampleTest {

    @Test
    void testOne() {
        System.out.println("Test One");
    }

    @Disabled
    @Test
    void testTwo() {
        System.out.println("Test Two");
    }

    @Test
    void testThree() {
        System.out.println("Test Three");
    }
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Count only tests that are not disabled.
❓ assertion
advanced
1:30remaining
Assertion Behavior in Disabled Tests
Consider a test method annotated with @Disabled that contains failing assertions. What will be the test result when running this test?
AThe test will be skipped and no assertion failures reported
BThe test will fail due to assertion errors
CThe test will pass regardless of assertions
DThe test will run but report warnings instead of failures
Attempts:
2 left
💡 Hint
Think about whether the test code runs at all when disabled.
🔧 Debug
advanced
2:00remaining
Why is a Disabled Test Still Running?
A developer added @Disabled to a test method, but the test still runs during the test suite execution. Which of the following is the most likely cause?
AThe test runner does not support <code>@Disabled</code>
BThe test method is missing the <code>@Test</code> annotation
CThe <code>@Disabled</code> annotation was imported from the wrong package
DThe test method is private instead of public
Attempts:
2 left
💡 Hint
Check the import statements for annotations.
❓ framework
expert
2:30remaining
Combining @Disabled with Conditional Test Execution
In JUnit 5, if a test method is annotated with both @Disabled and a conditional annotation like @EnabledOnOs(OS.WINDOWS), what will happen when running the tests on a Windows machine?
AJUnit will throw an error due to conflicting annotations
BThe test will be skipped because @Disabled takes precedence over conditional annotations
CThe test will run because the condition is met, ignoring @Disabled
DThe test will run only if both annotations allow it, so it runs on Windows
Attempts:
2 left
💡 Hint
Consider the priority of @Disabled in test execution.

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

  1. Step 1: Understand the role of @Disabled

    The @Disabled annotation tells JUnit to skip running the test method or class.
  2. Step 2: Compare with other options

    It does not mark tests as passed, run them multiple times, or affect speed.
  3. Final Answer:

    To skip a test without deleting its code -> Option B
  4. 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

  1. Step 1: Identify correct annotation and syntax

    JUnit uses @Disabled with an optional string reason in parentheses and quotes.
  2. 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.
  3. Final Answer:

    @Disabled("Not ready yet")\npublic void testFeature() {} -> Option D
  4. 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

  1. Step 1: Understand class-level @Disabled effect

    When @Disabled is on a class, all tests inside are skipped.
  2. 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.
  3. Final Answer:

    Both testAdd and testSubtract will be skipped with reason shown -> Option A
  4. Quick Check:

    Class-level @Disabled skips all tests = D [OK]
Hint: Class @Disabled skips all tests inside it [OK]
Common Mistakes:
  • Thinking only first test is skipped
  • Assuming tests run despite class @Disabled
  • Confusing @Disabled with @Test
4. You wrote this test method:
@Disabled("Feature incomplete")
public void testFeature() {
    assertEquals(5, 2 + 3);
}
But when you run tests, this method still runs. What is the likely problem?
medium
A. The method is missing the @Test annotation
B. The @Disabled annotation is misspelled
C. The test method is private instead of public
D. JUnit does not support @Disabled on methods

Solution

  1. Step 1: Check required annotations for test methods

    JUnit requires @Test annotation to recognize a method as a test.
  2. Step 2: Analyze given code

    The method has @Disabled but lacks @Test, so JUnit ignores it as a test and does not skip it.
  3. Final Answer:

    The method is missing the @Test annotation -> Option A
  4. Quick Check:

    Missing @Test means method not recognized as test [OK]
Hint: Always add @Test to test methods even if @Disabled is present [OK]
Common Mistakes:
  • Assuming @Disabled alone makes method a test
  • Ignoring method visibility rules
  • Thinking JUnit does not support @Disabled on methods
5. You want to temporarily disable some tests in a large test suite but still see the reason for skipping in reports. Which approach is best?
hard
A. Remove the @Test annotation from tests to skip
B. Comment out the test methods in the code
C. Add @Disabled("Reason") to each test method you want to skip
D. Use @Disabled on the whole test class without reasons

Solution

  1. Step 1: Understand how to skip tests with reasons

    Using @Disabled("Reason") on test methods skips them and shows the reason in reports.
  2. Step 2: Compare other options

    Commenting out or removing @Test hides tests but does not show skip reasons. Disabling whole class skips all tests without selective reasons.
  3. Final Answer:

    Add @Disabled("Reason") to each test method you want to skip -> Option C
  4. Quick Check:

    @Disabled with reason per method shows skip reason [OK]
Hint: Use @Disabled("reason") on methods to skip with explanation [OK]
Common Mistakes:
  • Commenting out tests instead of disabling
  • Removing @Test hides tests silently
  • Disabling whole class loses selective control
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard