0
0
JUnittesting~10 mins

Extension model overview 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 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'
AJUnitExtension
BTestExtension
CExtension
DMyExtension
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add .class after the extension name
Using the wrong class name in @ExtendWith
2fill in blank
medium

Complete 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'
ABeforeEachCallback
BTestExecutionExceptionHandler
CBeforeAllCallback
DAfterEachCallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using BeforeAllCallback which runs once before all tests
Confusing AfterEachCallback with BeforeEachCallback
3fill in blank
hard

Fix 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'
AMyExtension.class
BMyExtension
CMyExtension()
Dnew MyExtension()
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting .class after the extension class name
Trying to instantiate the extension inside @ExtendWith
4fill in blank
hard

Fill 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'
ABeforeEachCallback
BAfterAllCallback
CAfterEachCallback
DBeforeAllCallback
Attempts:
3 left
💡 Hint
Common Mistakes
Using BeforeAllCallback or AfterAllCallback instead of per-test callbacks
Implementing only one of the two interfaces
5fill in blank
hard

Fill 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'
AParameterResolver
BString.class
C"Hello"
DObject.class
Attempts:
3 left
💡 Hint
Common Mistakes
Returning wrong type in supportsParameter check
Not implementing ParameterResolver interface
Returning wrong value in resolveParameter