ParameterResolver defines the API for Extensions that wish to dynamically resolve parameters at runtime for test constructors or methods.
Java source and doc
Definition of ParameterResolverVersion: 6.0.1 package org.junit.jupiter.api.extension;
@API(status = STABLE, since = "5.0")
public interface ParameterResolver extends TestInstantiationAwareExtension {
boolean supportsParameter(ParameterContext parameterContext, 1
ExtensionContext extensionContext)
throws ParameterResolutionException;
Object resolveParameter(ParameterContext parameterContext, 2
ExtensionContext extensionContext)
throws ParameterResolutionException;
}
We have already seen an example in our Introduction to JUnit Extension tutorial, let's see another example here.
Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.*;
@ExtendWith(ParameterResolverTest.CustomResolver.class)
public class ParameterResolverTest {
@Test
void testInjection(String data) {
System.out.println("received: " + data);
}
static class CustomResolver implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext pc, ExtensionContext ec) {
return pc.getParameter().getType() == String.class;
}
@Override
public Object resolveParameter(ParameterContext pc, ExtensionContext ec) {
return "hello world";
}
}
}
Output$ mvn test -Dtest=ParameterResolverTest [INFO] Scanning for projects... [INFO] [INFO] ------< com.logicbig.example:junit-5-parameter-resolver-example >------- [INFO] Building junit-5-parameter-resolver-example 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-parameter-resolver-example --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-extensions\junit-5-parameter-resolver-example\src\main\resources [INFO] [INFO] --- compiler:3.13.0:compile (default-compile) @ junit-5-parameter-resolver-example --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-parameter-resolver-example --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-extensions\junit-5-parameter-resolver-example\src\test\resources [INFO] [INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ junit-5-parameter-resolver-example --- [INFO] Recompiling the module because of changed source code. [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file with javac [debug target 1.8] to target\test-classes [WARNING] bootstrap class path is not set in conjunction with -source 8 not setting the bootstrap class path may lead to class files that cannot run on JDK 8 --release 8 is recommended instead of -source 8 -target 1.8 because it sets the bootstrap class path automatically [WARNING] source value 8 is obsolete and will be removed in a future release [WARNING] target value 8 is obsolete and will be removed in a future release [WARNING] To suppress warnings about obsolete options, use -Xlint:-options. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-parameter-resolver-example --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ParameterResolverTest received: hello world [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.058 s -- in com.logicbig.example.ParameterResolverTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.037 s [INFO] Finished at: 2025-12-30T16:04:42+08:00 [INFO] ------------------------------------------------------------------------
The successful execution of the test shows that a String value was injected directly into the test method signature. This proves that ParameterResolver allows for powerful, type-based dependency injection, enabling tests to receive required data or services without manual instantiation.
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|