Challenge - 5 Problems
ParameterResolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Think about what the ParameterResolver returns for the String parameter.
✗ Incorrect
The ParameterResolver supports String parameters and returns "Hello". The test prints this value, so output is "Hello".
❓ assertion
intermediate2: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? } }
Attempts:
2 left
💡 Hint
Check which assertion compares values correctly for Integer objects.
✗ Incorrect
assertEquals(42, number) correctly checks the Integer value. assertSame compares object references, which may fail due to boxing.
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
Check the difference between primitive types and wrapper classes in Java.
✗ Incorrect
supportsParameter checks for Double.class but the parameter is primitive double, so it returns false and injection fails.
🧠 Conceptual
advanced2:00remaining
ParameterResolver lifecycle and caching
Which statement about the lifecycle of a ParameterResolver instance in JUnit 5 is correct?
Attempts:
2 left
💡 Hint
Think about how JUnit 5 manages extensions and test instances.
✗ Incorrect
ParameterResolver instances are created once per test class and reused across all test methods within that class.
❓ framework
expert3: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"; } }
Attempts:
2 left
💡 Hint
Use ParameterContext methods to check annotation and type.
✗ Incorrect
pc.isAnnotated checks for annotation presence correctly. Using == for Class comparison is valid. This option uses recommended API methods.