0
0
JUnittesting~20 mins

ParameterResolver extension in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ParameterResolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of ParameterResolver supplying a String
What is the output of the following JUnit 5 test when using a ParameterResolver that always provides the string "Hello"?
JUnit
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.*;

class HelloParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.getParameter().getType() == String.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return "Hello";
    }
}

@ExtendWith(HelloParameterResolver.class)
public class GreetingTest {
    @Test
    void testGreeting(String greeting) {
        System.out.println(greeting);
    }
}
Anull
BTest fails with ParameterResolutionException
CCompilation error
DHello
Attempts:
2 left
💡 Hint
Think about what the ParameterResolver returns for the String parameter.
assertion
intermediate
2:00remaining
Assertion on injected parameter value
Given a ParameterResolver that injects an Integer with value 42, which assertion correctly verifies the injected value in a test method?
JUnit
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;

class IntParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.getParameter().getType() == Integer.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return 42;
    }
}

@ExtendWith(IntParameterResolver.class)
public class NumberTest {
    @Test
    void testNumber(Integer number) {
        // Which assertion is correct here?
    }
}
AassertSame(42, number);
BassertTrue(number == 42);
CassertEquals(42, number);
DassertNotNull(number);
Attempts:
2 left
💡 Hint
Check which assertion compares values correctly for Integer objects.
🔧 Debug
advanced
2:00remaining
Why does ParameterResolver fail to inject?
Consider this ParameterResolver implementation that tries to inject a Double but the test method parameter is of type double (primitive). Why does the injection fail?
JUnit
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.*;

class DoubleParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.getParameter().getType() == Double.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return 3.14;
    }
}

@ExtendWith(DoubleParameterResolver.class)
public class PiTest {
    @Test
    void testPi(double value) {
        System.out.println(value);
    }
}
AsupportsParameter returns false because double primitive != Double class
BresolveParameter returns wrong type causing ClassCastException
CTest method parameter must be Double, not double
DParameterResolver interface is not implemented correctly
Attempts:
2 left
💡 Hint
Check the difference between primitive types and wrapper classes in Java.
🧠 Conceptual
advanced
2:00remaining
ParameterResolver lifecycle and caching
Which statement about the lifecycle of a ParameterResolver instance in JUnit 5 is correct?
AParameterResolver instances are created once per test class and reused
BParameterResolver instances are singletons shared across all tests
CJUnit creates a new ParameterResolver instance per parameter injection
DA new ParameterResolver instance is created for each test method invocation
Attempts:
2 left
💡 Hint
Think about how JUnit 5 manages extensions and test instances.
framework
expert
3:00remaining
Custom ParameterResolver with conditional support
You want to create a ParameterResolver that only supports parameters annotated with @InjectValue and of type String. Which implementation of supportsParameter is correct?
JUnit
import org.junit.jupiter.api.extension.*;
import java.lang.reflect.Parameter;

@interface InjectValue {}

class ConditionalParameterResolver implements ParameterResolver {
    @Override
    public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
        return pc.isAnnotated(InjectValue.class) && pc.getParameter().getType() == String.class;
    }

    @Override
    public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
        return "Injected";
    }
}
Areturn pc.getParameter().getAnnotation(InjectValue.class) != null && pc.getParameter().getType().equals(String.class);
Breturn pc.isAnnotated(InjectValue.class) && pc.getParameter().getType() == String.class;
Creturn pc.getParameter().isAnnotationPresent(InjectValue.class) && pc.getParameter().getType() == String.class;
Dreturn pc.isAnnotated(InjectValue.class) && pc.getParameter().getType().equals(String.class);
Attempts:
2 left
💡 Hint
Use ParameterContext methods to check annotation and type.