The ParameterResolver interface requires implementing supportsParameter to check if a parameter is supported, and resolveParameter to provide the value.
Step 2: Eliminate incorrect method pairs
Options A, B, and C list methods unrelated to ParameterResolver interface.
Final Answer:
supportsParameter and resolveParameter -> Option A
3. Given this ParameterResolver implementation, what will be printed when the test runs?
class MyResolver implements ParameterResolver {
public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
return pc.getParameter().getType() == String.class;
}
public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
return "InjectedValue";
}
}
@ExtendWith(MyResolver.class)
class TestClass {
@Test
void testMethod(String param) {
System.out.println(param);
}
}
medium
A. null
B. InjectedValue
C. Compilation error
D. Test fails with exception
Solution
Step 1: Analyze supportsParameter method
It returns true only if the parameter type is String, so the test method's String param is supported.
Step 2: Analyze resolveParameter method
It returns the string "InjectedValue", so this value will be injected into the test method parameter.
Final Answer:
InjectedValue -> Option B
Quick Check:
Parameter injection prints InjectedValue [OK]
Hint: Check supportsParameter return true for parameter type [OK]
Common Mistakes:
Assuming parameter is null if not supported
Expecting compilation error due to interface methods
Thinking test fails without explicit parameter value
4. Identify the error in this ParameterResolver implementation:
class FaultyResolver implements ParameterResolver {
public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
return pc.getParameter().getType() == Integer.class;
}
public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
return 42;
}
}
@ExtendWith(FaultyResolver.class)
class TestClass {
@Test
void testMethod(int param) {
System.out.println(param);
}
}
medium
A. Missing @Override annotations cause runtime error
B. resolveParameter returns wrong type
C. supportsParameter checks for Integer.class but parameter is primitive int
D. Test method parameter must be Integer, not int
Solution
Step 1: Check parameter type in supportsParameter
The test method parameter is primitive int, but supportsParameter checks for Integer.class, which is the wrapper class. This causes supportsParameter to return false.
Step 2: Understand consequence of mismatch
Because supportsParameter returns false, resolveParameter is never called, so the parameter is not injected, causing test failure or default value usage.
Final Answer:
supportsParameter checks for Integer.class but parameter is primitive int -> Option C
Quick Check:
Primitive vs wrapper mismatch breaks supportsParameter [OK]
Hint: Match primitive and wrapper types carefully in supportsParameter [OK]
Common Mistakes:
Ignoring difference between int and Integer
Assuming @Override annotations are mandatory
Thinking resolveParameter type must differ
5. You want to create a ParameterResolver that injects a unique UUID string into any test method parameter of type String. Which implementation correctly achieves this and ensures a new UUID is generated for each test invocation?
hard
A. In resolveParameter, return UUID.randomUUID().toString() without caching
B. In supportsParameter, cache a UUID and return it for all calls
C. Return a fixed UUID string in resolveParameter for all tests
D. Generate UUID once in constructor and return it in resolveParameter
Solution
Step 1: Understand requirement for unique UUID per test
Each test invocation should get a new UUID string, so the UUID must be generated fresh every time resolveParameter is called.
Step 2: Evaluate options for UUID generation
In resolveParameter, return UUID.randomUUID().toString() without caching generates a new UUID string on each call, fulfilling the requirement. Options B, C, and D cache or fix the UUID, so they do not produce unique values per test.
Final Answer:
In resolveParameter, return UUID.randomUUID().toString() without caching -> Option A
Quick Check:
Generate UUID fresh in resolveParameter for uniqueness [OK]
Hint: Generate new UUID inside resolveParameter method [OK]
Common Mistakes:
Caching UUID and returning same value every time
Generating UUID once in constructor
Returning fixed string instead of UUID
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.