Test Overview
This test uses JUnit's ParameterResolver extension to inject a custom object into the test method. It verifies that the injected object is not null and has the expected property value.
This test uses JUnit's ParameterResolver extension to inject a custom object into the test method. It verifies that the injected object is not null and has the expected property value.
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolver; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class MyService { private final String name; MyService(String name) { this.name = name; } String getName() { return name; } } class MyServiceParameterResolver implements ParameterResolver { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return parameterContext.getParameter().getType() == MyService.class; } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return new MyService("InjectedService"); } } @ExtendWith(MyServiceParameterResolver.class) public class ParameterResolverTest { @Test void testInjectedService(MyService service) { assertNotNull(service, "Injected service should not be null"); assertEquals("InjectedService", service.getName(), "Service name should match injected value"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit test runner starts the test execution | Test environment initialized, extensions registered | — | PASS |
| 2 | JUnit detects test method 'testInjectedService' requires a MyService parameter | ParameterResolver extension is available | — | PASS |
| 3 | JUnit calls MyServiceParameterResolver.supportsParameter() to check if it can provide MyService | Parameter type is MyService | supportsParameter returns true | PASS |
| 4 | JUnit calls MyServiceParameterResolver.resolveParameter() to get MyService instance | MyService instance created with name 'InjectedService' | Returned object is instance of MyService | PASS |
| 5 | JUnit invokes testInjectedService with injected MyService instance | Test method running with parameter service = MyService('InjectedService') | assertNotNull(service) passes | PASS |
| 6 | JUnit verifies service.getName() equals 'InjectedService' | service.getName() returns 'InjectedService' | assertEquals('InjectedService', service.getName()) passes | PASS |
| 7 | Test method completes successfully | Test passed | — | PASS |
ParameterResolver in JUnit 5?ParameterResolver in JUnit 5?supportsParameter to check if a parameter is supported, and resolveParameter to provide the value.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);
}
}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);
}
}int, but supportsParameter checks for Integer.class, which is the wrapper class. This causes supportsParameter to return false.String. Which implementation correctly achieves this and ensures a new UUID is generated for each test invocation?resolveParameter is called.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.Pass the test and this pattern moves unknown