0
0
JUnittesting~10 mins

TestWatcher for reporting in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Astarted
Bpassed
Cskipped
Dfailed
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'passed' instead of 'failed' in the message.
Confusing method names for test lifecycle events.
2fill in blank
medium

Complete 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'
Afailed
Bpassed
Cskipped
Dstarted
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'failed' instead of 'passed' in the message.
Mixing up test lifecycle method names.
3fill in blank
hard

Fix 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'
Astarting
Bstarted
Cstart
DstartTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'started' instead of 'starting' as method name.
Using non-existent method names like 'startTest'.
4fill in blank
hard

Fill 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'
Asucceeded
Bstarting
Cfailed
Dfinished
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping succeeded and failed method names.
Using unrelated lifecycle method names.
5fill in blank
hard

Fill 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'
Astarting
Bsucceeded
Cfailed
Dfinished
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up method names or using 'finished' incorrectly.
Not overriding all three methods properly.