What if your tests could tell you their story without you lifting a finger?
Why TestWatcher for reporting in JUnit? - Purpose & Use Cases
Imagine running many tests one by one and writing down which tests passed or failed by hand.
You have to watch the screen constantly and note every detail yourself.
This manual way is slow and tiring.
You might miss some failures or write wrong results.
It is hard to keep track when tests grow in number.
TestWatcher automatically listens to test results and reports them for you.
It saves time and avoids mistakes by handling reporting behind the scenes.
System.out.println("Test passed: " + testName);new TestWatcher() {
@Override
protected void succeeded(Description d) { System.out.println(d.getMethodName() + " passed"); }
}It lets you focus on writing tests while getting clear, automatic reports on their results.
In a big project, TestWatcher helps teams quickly see which tests fail after code changes, speeding up fixes.
Manual test reporting is slow and error-prone.
TestWatcher automates result tracking and reporting.
This improves accuracy and saves time in testing.
Practice
TestWatcher in JUnit tests?Solution
Step 1: Understand TestWatcher role
TestWatcheris a JUnit class designed to listen to test lifecycle events such as when a test starts, succeeds, fails, or finishes.Step 2: Differentiate from other test tasks
Writing assertions, mocking, or configuring databases are separate concerns and not the main purpose ofTestWatcher.Final Answer:
To react to test lifecycle events like start, success, failure, and finish -> Option AQuick Check:
TestWatcher = react to test events [OK]
- Confusing TestWatcher with assertion writing
- Thinking TestWatcher creates mocks
- Assuming TestWatcher manages test setup like DB connections
TestWatcher method that runs when a test fails?Solution
Step 1: Recall TestWatcher method signature
Thefailedmethod inTestWatcherispublic void failed(ExtensionContext context, Throwable cause).Step 2: Check method modifiers and parameter order
It must be public, return void, and parameters areExtensionContextfirst, thenThrowable.Final Answer:
public void failed(ExtensionContext context, Throwable cause) -> Option AQuick Check:
failed method = public void failed(ExtensionContext, Throwable) [OK]
- Swapping parameter order
- Using protected instead of public
- Missing return type or wrong method name
TestWatcher subclass, what will be printed if the test exampleTest fails?class MyWatcher extends TestWatcher {
@Override
public void failed(ExtensionContext context, Throwable cause) {
context.getTestMethod().ifPresent(method -> System.out.println(method.getName() + " failed"));
}
}
@Test
@ExtendWith(MyWatcher.class)
void exampleTest() {
assertEquals(1, 2);
}Solution
Step 1: Understand what failed() prints
The overriddenfailedmethod prints the test method name plus " failed".Step 2: Identify test method name on failure
The test method is namedexampleTest, so the output will be "exampleTest failed".Final Answer:
exampleTest failed -> Option DQuick Check:
Print = methodName + " failed" = exampleTest failed [OK]
- Assuming no output on failure
- Printing only 'failed' without method name
- Confusing method name with test class name
TestWatcher subclass that aims to log test success:class SuccessWatcher extends TestWatcher {
@Override
public void succeeded(Description description) {
System.out.println(description.getDisplayName() + " succeeded");
}
}Solution
Step 1: Check JUnit version and method signature
In JUnit 5,TestWatchermethods useExtensionContextparameter, notDescription.Step 2: Identify correct succeeded() signature
The correct override ispublic void succeeded(ExtensionContext context)in JUnit 5.Final Answer:
Method signature is incorrect; it should be public void succeeded(ExtensionContext context) -> Option BQuick Check:
JUnit 5 TestWatcher uses ExtensionContext parameter [OK]
- Using JUnit 4 signatures in JUnit 5
- Wrong parameter types or order
- Confusing access modifiers
TestWatcher that logs test start and failure events, but only for tests whose names start with "critical". Which approach correctly implements this behavior in JUnit 5?Solution
Step 1: Identify correct JUnit 5 TestWatcher methods
In JUnit 5,TestWatchermethods to override arestarting(ExtensionContext)andfailed(ExtensionContext, Throwable).Step 2: Filter test names using context.getDisplayName()
Usecontext.getDisplayName()to get the test name and check if it starts with "critical" before logging.Final Answer:
Override starting(ExtensionContext context) and failed(ExtensionContext context, Throwable cause), check context.getDisplayName() starts with "critical" before logging -> Option CQuick Check:
JUnit 5 TestWatcher + ExtensionContext + name check = Overridestarting(ExtensionContext context)andfailed(ExtensionContext context, Throwable cause), checkcontext.getDisplayName()starts with "critical" before logging [OK]
- Using JUnit 4 method signatures in JUnit 5
- Checking wrong parameter types
- Using lifecycle methods not related to TestWatcher
Ready to prove it?
Pass the test and this pattern moves unknown
