junit-team/junit5 · error · JUnitException
Failed to inject parameter value into field: <field>
Error message
Failed to inject parameter value into field: <field>
What it means
Thrown by ResolverFacade.setField() when Field.set() fails during field injection for a @ParameterizedClass field annotated with @Parameter. After the argument is resolved and converted, JUnit reflectively sets it on the test instance; if this reflective write throws (IllegalAccessException, IllegalArgumentException, or a type mismatch after conversion), it is wrapped in this JUnitException.
Source
Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/ResolverFacade.java:313
invocationIndex, resolutionCache));
}
}
private Stream<ParameterDeclaration> getAllParameterDeclarations() {
return Stream.concat(this.indexedParameterDeclarations.declarationsByIndex.values().stream(),
aggregatorParameters.stream());
}
private void setField(Object testInstance, FieldParameterDeclaration declaration, ExtensionContext extensionContext,
EvaluatedArgumentSet arguments, int invocationIndex, ResolutionCache resolutionCache) {
Object argument = resolutionCache.resolve(declaration,
() -> resolve(declaration, extensionContext, arguments, invocationIndex, Optional.empty()));
try {
declaration.getField().set(testInstance, argument);
}
catch (Exception e) {
throw new JUnitException("Failed to inject parameter value into field: " + declaration.getField(), e);
}
}
private @Nullable Object resolve(ResolvableParameterDeclaration parameterDeclaration,
ExtensionContext extensionContext, EvaluatedArgumentSet arguments, int invocationIndex,
Optional<ParameterContext> parameterContext) {
Resolver resolver = getResolver(extensionContext, parameterDeclaration);
return parameterDeclaration.resolve(resolver, extensionContext, arguments, invocationIndex, parameterContext);
}
private Resolver getResolver(ExtensionContext extensionContext, ResolvableParameterDeclaration declaration) {
return this.resolvers.computeIfAbsent(declaration, __ -> this.aggregatorParameters.contains(declaration) //
? createAggregator(declaration, extensionContext) //
: createConverter(declaration, extensionContext));
}
private int toLogicalIndex(ParameterContext parameterContext) {
int index = parameterContext.getIndex() - this.parameterIndexOffset;View on GitHub (pinned to 956246301e)
Solutions
- Ensure the @Parameter field type matches what the argument source and converter produce
- Add a @ConvertWith converter if the default converter cannot handle the source-to-field-type mapping
- Check the nested exception (cause) for the exact Field.set() failure — IllegalArgumentException typically indicates a type mismatch
- Verify the CSV/value source column aligns with the @Parameter index
Example fix
// before
@ParameterizedClass
@CsvSource("hello, 2")
class MyTest {
@Parameter(0) int a; // 'hello' cannot convert to int
@Parameter(1) int b;
}
// after
@ParameterizedClass
@CsvSource("1, 2")
class MyTest {
@Parameter(0) int a;
@Parameter(1) int b;
} Defensive patterns
Strategy: try-catch
Validate before calling
// Ensure field types match argument source columns before running: // @Parameter(0) int a <-> CSV column 0 must be numeric // Validate by checking the source data type vs field type:
Try / catch
// This error occurs during test instance setup, not in user code. // Prevent by ensuring field types match converted argument types. // Check the nested cause for the exact Field.set failure.
Prevention
- Ensure @Parameter field types are compatible with the source data and any converters
- Do not declare @Parameter fields as incompatible types for the CSV/value source columns
- Add @ConvertWith converters when the default converter cannot handle the source-to-field mapping
- Check the nested exception cause for the specific IllegalArgumentException detail
When it happens
Trigger: A @ParameterizedClass with a @Parameter field whose declared type is incompatible with the converted argument value at runtime. Despite the field being made accessible, Field.set() throws IllegalArgumentException if the argument type does not match the field type after conversion.
Common situations: Declaring @Parameter(0) on a field of type int but the CSV source provides a non-numeric string that was not properly converted. A custom @ConvertWith converter returns a type incompatible with the field. Security manager restrictions on reflection (rare in modern JVMs).
Related errors
- Configuration error: <error>.
- %d configuration errors:%n%s
- Constructor injection is not supported for @ParameterizedCla
- Argument at index [%d] with value [%s] and type [%s] could n
- No built-in converter for source type %s and target type %s
AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04).
Data as JSON: /data/errors/e32773995690f1ac.json.
Report an issue: GitHub.