TestWatcher is a JUnit extension that allows you to add custom behavior when tests succeed, fail, or are skipped. It helps in reporting and logging test results.
Click to reveal answer
beginner
How do you use TestWatcher to log a message when a test fails?
Override the <code>failed(Throwable e, Description description)</code> method in a TestWatcher subclass to add custom logging or reporting when a test fails.
Click to reveal answer
beginner
Which method in TestWatcher is called when a test succeeds?
The succeeded(Description description) method is called when a test passes successfully.
Click to reveal answer
intermediate
Why use TestWatcher instead of @After or @Before methods for reporting?
TestWatcher provides hooks specifically for test result events (success, failure, skip), making it easier and cleaner to add reporting logic tied directly to test outcomes.
Click to reveal answer
intermediate
Show a simple example of a TestWatcher that prints test start and finish messages.
Hint: JUnit 5 TestWatcher methods use ExtensionContext, not Description [OK]
Common Mistakes:
Using JUnit 4 signatures in JUnit 5
Wrong parameter types or order
Confusing access modifiers
5. You want to create a custom 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?
hard
A. Override starting(Description description) and failed(Throwable e, Description description), check description.getMethodName() starts with "critical" before logging
B. Override beforeTestExecution(ExtensionContext context) and afterTestExecution(ExtensionContext context), check context.getTestMethod().getName() starts with "critical" before logging
C. Override starting(ExtensionContext context) and failed(ExtensionContext context, Throwable cause), check context.getDisplayName() starts with "critical" before logging
D. Override testStarted(Description description) and testFailure(Failure failure), check failure.getDescription().getMethodName() starts with "critical" before logging
In JUnit 5, TestWatcher methods to override are starting(ExtensionContext) and failed(ExtensionContext, Throwable).
Step 2: Filter test names using context.getDisplayName()
Use context.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 C
Quick Check:
JUnit 5 TestWatcher + ExtensionContext + name check = Override starting(ExtensionContext context) and failed(ExtensionContext context, Throwable cause), check context.getDisplayName() starts with "critical" before logging [OK]
Hint: Use ExtensionContext methods and check displayName for filtering [OK]
Common Mistakes:
Using JUnit 4 method signatures in JUnit 5
Checking wrong parameter types
Using lifecycle methods not related to TestWatcher
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.