0
0
JUnittesting~15 mins

ParameterResolver extension in JUnit - Build an Automation Script

Choose your learning style9 modes available
Automate a JUnit test using ParameterResolver to inject a custom object
Preconditions (3)
Step 1: Create a ParameterResolver class that implements ParameterResolver interface
Step 2: Implement supportsParameter to return true for the custom object type
Step 3: Implement resolveParameter to return an instance of the custom object
Step 4: Create a test class and register the ParameterResolver extension using @ExtendWith
Step 5: Write a test method that accepts the custom object as a parameter
Step 6: Inside the test method, verify the custom object is not null and has expected properties
✅ Expected Result: The test runs successfully with the custom object injected by the ParameterResolver, and assertions pass confirming the object is correctly provided
Automation Requirements - JUnit 5
Assertions Needed:
Assert that the injected parameter is not null
Assert that the injected parameter has expected property values
Best Practices:
Use @ExtendWith to register the ParameterResolver
Implement ParameterResolver interface correctly with supportsParameter and resolveParameter
Keep the ParameterResolver logic simple and focused
Use assertions from org.junit.jupiter.api.Assertions
Write clear and descriptive test method names
Automated Solution
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.*;

// Custom object to be injected
class CustomObject {
    private final String value;

    public CustomObject(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

// ParameterResolver implementation
class CustomObjectParameterResolver implements ParameterResolver {

    @Override
    public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
        return parameterContext.getParameter().getType() == CustomObject.class;
    }

    @Override
    public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
        return new CustomObject("InjectedValue");
    }
}

@ExtendWith(CustomObjectParameterResolver.class)
public class CustomObjectParameterResolverTest {

    @Test
    void testInjectedCustomObject(CustomObject customObject) {
        assertNotNull(customObject, "CustomObject should be injected and not null");
        assertEquals("InjectedValue", customObject.getValue(), "CustomObject value should match the injected value");
    }
}

The code defines a CustomObject class with a simple string property.

The CustomObjectParameterResolver class implements JUnit 5's ParameterResolver interface. It overrides supportsParameter to check if the parameter type is CustomObject. It overrides resolveParameter to return a new CustomObject instance with a fixed value.

The test class CustomObjectParameterResolverTest is annotated with @ExtendWith to register the resolver. The test method testInjectedCustomObject accepts a CustomObject parameter, which JUnit injects automatically using the resolver.

Assertions check that the injected object is not null and that its value matches the expected string. This confirms the ParameterResolver works correctly.

Common Mistakes - 4 Pitfalls
Not implementing supportsParameter correctly
{'mistake': 'Forgetting to register the ParameterResolver with @ExtendWith', 'why_bad': "JUnit will not apply the resolver and parameters won't be injected", 'correct_approach': 'Always annotate the test class with @ExtendWith and specify your ParameterResolver class'}
Returning null from resolveParameter
Using ParameterResolver for unsupported parameter types
Bonus Challenge

Now add data-driven testing by injecting different CustomObject instances with different values for three test runs

Show Hint