ParameterResolver helps tests get needed values automatically. It makes tests cleaner and easier to read.
0
0
ParameterResolver extension in JUnit
Introduction
When you want to provide custom objects or values to test methods without manual setup.
When multiple tests need the same setup data or resource injected.
When you want to avoid repeating code to create test parameters.
When you want to control how parameters are created for tests.
When you want to inject mocks or special objects into test methods.
Syntax
JUnit
public class MyParameterResolver implements ParameterResolver { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { // Return true if this resolver supports the parameter return false; // placeholder implementation } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { // Return the object to inject return null; // placeholder implementation } }
The supportsParameter method checks if the resolver can provide a value for a parameter.
The resolveParameter method returns the actual value to inject into the test.
Examples
This resolver injects the string "Injected String" whenever a test method asks for a String parameter.
JUnit
public class StringParameterResolver implements ParameterResolver { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return parameterContext.getParameter().getType() == String.class; } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return "Injected String"; } }
The test method receives the injected string automatically from the resolver.
JUnit
@ExtendWith(StringParameterResolver.class) public class MyTest { @Test void testWithInjectedString(String injected) { assertEquals("Injected String", injected); } }
Sample Program
This example shows a ParameterResolver that injects the number 42 into test methods that ask for an Integer. The test checks the value and prints it.
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolver; import static org.junit.jupiter.api.Assertions.assertEquals; public class ParameterResolverExample { public static class NumberParameterResolver implements ParameterResolver { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return parameterContext.getParameter().getType() == Integer.class; } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { return 42; } } @ExtendWith(NumberParameterResolver.class) public static class MyTest { @Test void testInjectedNumber(Integer number) { assertEquals(42, number); System.out.println("Injected number is: " + number); } } }
OutputSuccess
Important Notes
ParameterResolver helps avoid manual setup in tests.
Always check parameter type carefully in supportsParameter.
Use @ExtendWith to apply your resolver to test classes.
Summary
ParameterResolver injects values into test method parameters automatically.
It requires implementing two methods: supportsParameter and resolveParameter.
Use it to make tests cleaner and reduce repeated setup code.