0
0
JUnittesting~10 mins

ParameterResolver extension in JUnit - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit 5
JUnit
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");
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1JUnit test runner starts the test executionTest environment initialized, extensions registered-PASS
2JUnit detects test method 'testInjectedService' requires a MyService parameterParameterResolver extension is available-PASS
3JUnit calls MyServiceParameterResolver.supportsParameter() to check if it can provide MyServiceParameter type is MyServicesupportsParameter returns truePASS
4JUnit calls MyServiceParameterResolver.resolveParameter() to get MyService instanceMyService instance created with name 'InjectedService'Returned object is instance of MyServicePASS
5JUnit invokes testInjectedService with injected MyService instanceTest method running with parameter service = MyService('InjectedService')assertNotNull(service) passesPASS
6JUnit verifies service.getName() equals 'InjectedService'service.getName() returns 'InjectedService'assertEquals('InjectedService', service.getName()) passesPASS
7Test method completes successfullyTest passed-PASS
Failure Scenario
Failing Condition: ParameterResolver does not support the parameter type or returns null
Execution Trace Quiz - 3 Questions
Test your understanding
What does the ParameterResolver do in this test?
AIt creates and injects a MyService instance into the test method
BIt runs the test method automatically
CIt asserts the test results
DIt configures the test environment
Key Result
Using ParameterResolver allows clean injection of dependencies into test methods, improving test readability and reusability.