0
0
JUnittesting~20 mins

TestWatcher for reporting in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TestWatcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TestWatcher override?
Consider this JUnit 5 TestWatcher extension code. What will be printed when the test fails?
JUnit
import org.junit.jupiter.api.extension.TestWatcher;
import org.junit.jupiter.api.extension.ExtensionContext;

public class MyWatcher implements TestWatcher {
    @Override
    public void testFailed(ExtensionContext context, Throwable cause) {
        System.out.println("Test failed: " + context.getDisplayName());
    }
}

// Assume this watcher is registered for a test named "exampleTest" which fails.
ATest failed: exampleTest
BTest failed: null
CNo output printed
DCompilation error due to missing method
Attempts:
2 left
💡 Hint
The testFailed method prints the test name on failure.
assertion
intermediate
2:00remaining
Which assertion correctly verifies a test was skipped using TestWatcher?
You want to assert inside a TestWatcher that a test was skipped. Which assertion is correct?
JUnit
import org.junit.jupiter.api.extension.TestWatcher;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.util.Optional;

public class SkipWatcher implements TestWatcher {
    @Override
    public void testSkipped(ExtensionContext context, Optional<String> reason) {
        // Which assertion below is correct?
    }
}
AassertFalse(reason.isPresent());
BassertEquals("Skipped", reason.get());
CassertTrue(reason.isPresent());
DassertNull(reason);
Attempts:
2 left
💡 Hint
The reason parameter is Optional and should be present if skipped.
🔧 Debug
advanced
2:00remaining
Why does this TestWatcher not print on test success?
This TestWatcher override does not print anything when a test passes. Why? public class SilentWatcher implements TestWatcher { @Override public void testSuccessful(ExtensionContext context) { // Intentionally left blank } }
ABecause testSuccessful is never called on success
BBecause ExtensionContext is null on success
CBecause TestWatcher requires calling super.testSuccessful() to print
DBecause testSuccessful method is empty, no output is printed
Attempts:
2 left
💡 Hint
Check what the method implementation does.
🧠 Conceptual
advanced
2:00remaining
What is the main purpose of TestWatcher in JUnit 5?
Choose the best description of what TestWatcher is used for in JUnit 5 testing.
ATo replace assertions inside test methods
BTo monitor test lifecycle events and perform actions on test success, failure, or skip
CTo configure test execution order
DTo mock dependencies during tests
Attempts:
2 left
💡 Hint
Think about what events TestWatcher listens to.
framework
expert
2:00remaining
How to register a TestWatcher extension for all tests in a class?
You want to apply a TestWatcher implementation to all tests in a JUnit 5 test class. Which is the correct way?
A@ExtendWith(MyWatcher.class) on the test class
BImplement TestWatcher interface in the test class directly
CCall MyWatcher.testFailed() manually in each test
DRegister MyWatcher in a static block inside the test class
Attempts:
2 left
💡 Hint
JUnit 5 uses annotations to register extensions.