Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to implement the ParameterResolver interface.
JUnit
public class MyParameterResolver implements ParameterResolver { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return parameterContext.getParameter().getType() == [1]; } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return "Hello, JUnit!"; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class type in supportsParameter.
Forgetting to use .class after the type.
✗ Incorrect
The supportsParameter method should check if the parameter type is String.class to provide a String value.
2fill in blank
mediumComplete the test method to use the ParameterResolver extension.
JUnit
@ExtendWith(MyParameterResolver.class) public class MyTest { @Test void testWithParameter([1] message) { assertEquals("Hello, JUnit!", message); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter type not supported by the resolver.
Forgetting to annotate the test class with @ExtendWith.
✗ Incorrect
The test method parameter must be String to match the ParameterResolver's supported type.
3fill in blank
hardFix the error in the supportsParameter method to correctly check for Integer type.
JUnit
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return parameterContext.getParameter().getType() == [1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int.class instead of Integer.class.
Confusing primitive and wrapper types.
✗ Incorrect
Use Integer.class to check for the Integer wrapper type, not int.class which is primitive.
4fill in blank
hardFill both blanks to correctly implement resolveParameter returning a fixed integer value.
JUnit
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return [1];
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return parameterContext.getParameter().getType() == [2];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a String instead of an Integer.
Checking for String.class instead of Integer.class.
✗ Incorrect
The resolver returns 42 as an Integer, so supportsParameter must check for Integer.class.
5fill in blank
hardFill all three blanks to create a ParameterResolver that provides a List<String> with one element "test".
JUnit
public class ListParameterResolver implements ParameterResolver { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return parameterContext.getParameter().getType() == [1]; } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return [2]; } } // Usage in test: @ExtendWith(ListParameterResolver.class) public class ListTest { @Test void testListParameter([3] list) { assertEquals(1, list.size()); assertEquals("test", list.get(0)); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ArrayList.class instead of List.class in supportsParameter.
Returning an empty list or wrong list content.
Using raw List instead of List in test method.
✗ Incorrect
The supportsParameter checks for List.class, resolveParameter returns List.of("test"), and the test method parameter is List.