0
0
JUnittesting~10 mins

ParameterResolver extension 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 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'
AObject.class
Bint.class
CList.class
DString.class
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class type in supportsParameter.
Forgetting to use .class after the type.
2fill in blank
medium

Complete 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'
Aint
BList<String>
CString
DObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter type not supported by the resolver.
Forgetting to annotate the test class with @ExtendWith.
3fill in blank
hard

Fix 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'
AInteger.class
Bint.class
CLong.class
DDouble.class
Attempts:
3 left
💡 Hint
Common Mistakes
Using int.class instead of Integer.class.
Confusing primitive and wrapper types.
4fill in blank
hard

Fill 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'
A42
BString.class
CInteger.class
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a String instead of an Integer.
Checking for String.class instead of Integer.class.
5fill in blank
hard

Fill 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'
AList.class
BList.of("test")
CList<String>
DArrayList.class
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.