0
0
JUnittesting~10 mins

Custom extensions 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 register a custom JUnit 5 extension using the @ExtendWith annotation.

JUnit
@ExtendWith([1].class)
public class MyTest {
    @Test
    void testSomething() {
        // test code
    }
}
Drag options to blanks, or click blank then click option'
AJUnitExtension
BTestExtension
CExtensionPoint
DMyExtension
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name that does not exist as an extension.
Forgetting to add .class after the extension class name.
2fill in blank
medium

Complete the code to implement a custom JUnit 5 extension by overriding the beforeEach method.

JUnit
public class MyExtension implements BeforeEachCallback {
    @Override
    public void [1](ExtensionContext context) throws Exception {
        System.out.println("Before each test");
    }
}
Drag options to blanks, or click blank then click option'
AbeforeEach
BbeforeAll
CafterEach
DafterAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using afterEach instead of beforeEach.
Using beforeAll which belongs to a different interface.
3fill in blank
hard

Fix the error in the custom extension code by completing the missing method name.

JUnit
public class MyExtension implements AfterTestExecutionCallback {
    @Override
    public void [1](ExtensionContext context) throws Exception {
        System.out.println("After test execution");
    }
}
Drag options to blanks, or click blank then click option'
AafterTestExecution
BafterAll
CafterEach
DbeforeTestExecution
Attempts:
3 left
💡 Hint
Common Mistakes
Using afterEach which belongs to a different interface.
Using beforeTestExecution which is not part of this interface.
4fill in blank
hard

Fill both blanks to create a custom extension that implements both BeforeAllCallback and AfterAllCallback interfaces.

JUnit
public class MyExtension implements [1], [2] {
    @Override
    public void beforeAll(ExtensionContext context) throws Exception {
        System.out.println("Before all tests");
    }

    @Override
    public void afterAll(ExtensionContext context) throws Exception {
        System.out.println("After all tests");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeAllCallback
BBeforeEachCallback
CAfterAllCallback
DAfterEachCallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using BeforeEachCallback or AfterEachCallback which run before/after each test, not all tests.
Forgetting to implement both interfaces.
5fill in blank
hard

Fill all three blanks to create a custom extension that logs test start and end times using BeforeTestExecutionCallback and AfterTestExecutionCallback.

JUnit
public class TimingExtension implements [1], [2] {
    private long startTime;

    @Override
    public void beforeTestExecution(ExtensionContext context) throws Exception {
        startTime = System.currentTimeMillis();
        System.out.println("Test started at: " + startTime);
    }

    @Override
    public void [3](ExtensionContext context) throws Exception {
        long endTime = System.currentTimeMillis();
        System.out.println("Test ended at: " + endTime);
        System.out.println("Duration: " + (endTime - startTime) + " ms");
    }
}
Drag options to blanks, or click blank then click option'
ABeforeTestExecutionCallback
BAfterTestExecutionCallback
CafterTestExecution
DbeforeTestExecution
Attempts:
3 left
💡 Hint
Common Mistakes
Using beforeTestExecution as the method name for after test execution.
Not implementing both interfaces.