Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to extend TestWatcher for custom test reporting.
JUnit
class MyTestWatcher extends TestWatcher { @Override protected void failed(Throwable e, Description description) { System.out.println("Test " + description.getMethodName() + " [1]."); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'passed' instead of 'failed' in the message.
Confusing method names for test lifecycle events.
✗ Incorrect
The failed() method is called when a test fails, so the message should say 'failed'.
2fill in blank
mediumComplete the code to print a message when a test succeeds.
JUnit
class MyTestWatcher extends TestWatcher { @Override protected void succeeded(Description description) { System.out.println("Test " + description.getMethodName() + " [1] successfully."); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'failed' instead of 'passed' in the message.
Mixing up test lifecycle method names.
✗ Incorrect
The succeeded() method runs when a test passes, so the message should say 'passed'.
3fill in blank
hardFix the error in the method signature to override the TestWatcher method correctly.
JUnit
class MyTestWatcher extends TestWatcher { @Override protected void [1](Description description) { System.out.println("Test started: " + description.getMethodName()); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'started' instead of 'starting' as method name.
Using non-existent method names like 'startTest'.
✗ Incorrect
The correct method name to override for test start is starting(Description description).
4fill in blank
hardFill both blanks to log test success and failure messages correctly.
JUnit
class MyTestWatcher extends TestWatcher { @Override protected void [1](Description description) { System.out.println("Test " + description.getMethodName() + " passed."); } @Override protected void [2](Throwable e, Description description) { System.out.println("Test " + description.getMethodName() + " failed."); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping succeeded and failed method names.
Using unrelated lifecycle method names.
✗ Incorrect
The succeeded() method is for passed tests, and failed() is for failed tests.
5fill in blank
hardFill all three blanks to create a TestWatcher that reports test start, success, and failure.
JUnit
class MyTestWatcher extends TestWatcher { @Override protected void [1](Description description) { System.out.println("Starting test: " + description.getMethodName()); } @Override protected void [2](Description description) { System.out.println("Test passed: " + description.getMethodName()); } @Override protected void [3](Throwable e, Description description) { System.out.println("Test failed: " + description.getMethodName()); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up method names or using 'finished' incorrectly.
Not overriding all three methods properly.
✗ Incorrect
The starting() method runs when a test starts, succeeded() when it passes, and failed() when it fails.