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 |