Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to register a JUnit 5 extension using the annotation.
JUnit
@ExtendWith([1].class) public class MyTest { @Test void testExample() { // test code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add .class after the extension name
Using the wrong class name in @ExtendWith
✗ Incorrect
The @ExtendWith annotation requires the class name of the extension to register it. Here, 'MyExtension' is the correct extension class.
2fill in blank
mediumComplete the code to implement the JUnit 5 extension interface for before each test callback.
JUnit
public class MyExtension implements [1] { @Override public void beforeEach(ExtensionContext context) throws Exception { // setup code } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BeforeAllCallback which runs once before all tests
Confusing AfterEachCallback with BeforeEachCallback
✗ Incorrect
To run code before each test, implement the BeforeEachCallback interface.
3fill in blank
hardFix the error in the extension registration code to properly enable the extension.
JUnit
@ExtendWith([1]) public class SampleTest { @Test void testSomething() {} }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting .class after the extension class name
Trying to instantiate the extension inside @ExtendWith
✗ Incorrect
The @ExtendWith annotation requires the class literal with .class, not an instance or just the class name.
4fill in blank
hardFill both blanks to create a custom extension that implements both before and after each test callbacks.
JUnit
public class CustomExtension implements [1], [2] { @Override public void beforeEach(ExtensionContext context) throws Exception { // before test } @Override public void afterEach(ExtensionContext context) throws Exception { // after test } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BeforeAllCallback or AfterAllCallback instead of per-test callbacks
Implementing only one of the two interfaces
✗ Incorrect
To run code before and after each test, implement BeforeEachCallback and AfterEachCallback interfaces.
5fill in blank
hardFill all three blanks to create a parameter resolver extension that supports injecting a String parameter with value "Hello".
JUnit
public class StringParameterResolver implements [1] { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return parameterContext.getParameter().getType() == [2]; } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return [3]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning wrong type in supportsParameter check
Not implementing ParameterResolver interface
Returning wrong value in resolveParameter
✗ Incorrect
To create a parameter resolver, implement ParameterResolver. Check if parameter type is String.class and return "Hello" as the resolved value.