Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The @ExtendWith annotation requires the class name of the custom extension. Here, 'MyExtension' is the correct custom extension class.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using afterEach instead of beforeEach.
Using beforeAll which belongs to a different interface.
✗ Incorrect
The BeforeEachCallback interface requires implementing the beforeEach method to run code before each test.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using afterEach which belongs to a different interface.
Using beforeTestExecution which is not part of this interface.
✗ Incorrect
The AfterTestExecutionCallback interface requires the method afterTestExecution to be implemented.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BeforeEachCallback or AfterEachCallback which run before/after each test, not all tests.
Forgetting to implement both interfaces.
✗ Incorrect
To run code before all tests and after all tests, implement BeforeAllCallback and AfterAllCallback interfaces.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using beforeTestExecution as the method name for after test execution.
Not implementing both interfaces.
✗ Incorrect
The class implements BeforeTestExecutionCallback and AfterTestExecutionCallback interfaces. The method afterTestExecution is overridden to log the end time.