junit-team/junit5 · error · ArgumentConversionException
cannot convert to type [ ]. Only target type [ ] is…
Error message
%s cannot convert to type [%s]. Only target type [%s] is supported.
What it means
Thrown by TypedArgumentConverter when the declared targetType (T) is not assignable to the actual target type required at the injection site (context.getField().getType()). The converter is bound to a specific output type; if the field/parameter expects a wider or different type, conversion cannot proceed safely. The message prints actualTargetType and targetType.
Solutions
- Make the parameter/field type match (or be a supertype of) the converter's declared targetType.
- Update the converter's targetType generic to the type actually required by the parameter.
- Ensure ReflectionUtils.isAssignableTo(converter.targetType, parameterType) holds for the pair.
Example fix
// before
class ToLocalDate extends TypedArgumentConverter<String, LocalDate> { ... }
void test(@ConvertWith(ToLocalDate.class) LocalDateTime v) { }
// after
class ToLocalDateTime extends TypedArgumentConverter<String, LocalDateTime> { ... }
void test(@ConvertWith(ToLocalDateTime.class) LocalDateTime v) { } Defensive patterns
Strategy: validation
Validate before calling
// Ensure converter target type is assignable to the parameter type
Class<?> targetType = LocalDateTime.class; // converter's declared T
Class<?> paramType = fieldOrParameter.getType();
if (!targetType.isAssignableFrom(paramType) && !paramType.isAssignableFrom(targetType)) {
throw new IllegalStateException("Converter target " + targetType + " incompatible with " + paramType);
} Type guard
static <S, T> boolean targetCompatible(TypedArgumentConverter<S, T> c, Class<?> actualTarget) {
return org.junit.platform.commons.util.ReflectionUtils.isAssignableTo(
c.getTargetType(), actualTarget);
} Prevention
- Keep the converter's targetType generic equal to the parameter's declared type.
- When refactoring a converter's generics, update every usage site.
- Prefer widening the parameter type only to a true supertype of the converter's output.
When it happens
Trigger: A TypedArgumentConverter<String, LocalDate> is registered on a parameter or field whose declared type is LocalDateTime (or Object that the framework resolves to an incompatible concrete type). The `!ReflectionUtils.isAssignableTo(this.targetType, actualTargetType)` guard fires.
Common situations: Registering a converter that produces a subtype on a parameter declared as a sibling type. Refactoring the converter's targetType generic without updating the parameter signature. Mismatch between @ParameterizedTest parameter type and the converter's output generic.
Related errors
- cannot convert objects of type [ ]. Only source objects of…
- Cannot convert to
- @EmptySource cannot provide an empty argument
- AnnotationBasedArgumentsProvider does not override the…
- Argument at index [ ] with value [ ] and type [ ] could not…
AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11).
Data as JSON: /api/errors/cef2814e4d05e7bb.
Report an issue: GitHub.
Appendix: source
Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/TypedArgumentConverter.java:79
public final @Nullable Object convert(@Nullable Object source, FieldContext context)
throws ArgumentConversionException {
return convert(source, context.getField().getType());
}
private T convert(@Nullable Object source, Class<?> actualTargetType) {
if (source == null) {
return convert(null);
}
if (!this.sourceType.isInstance(source)) {
String message = "%s cannot convert objects of type [%s]. Only source objects of type [%s] are supported.".formatted(
getClass().getSimpleName(), source.getClass().getTypeName(), this.sourceType.getTypeName());
throw new ArgumentConversionException(message);
}
if (!ReflectionUtils.isAssignableTo(this.targetType, actualTargetType)) {
String message = "%s cannot convert to type [%s]. Only target type [%s] is supported.".formatted(
getClass().getSimpleName(), actualTargetType.getTypeName(), this.targetType.getTypeName());
throw new ArgumentConversionException(message);
}
return convert(this.sourceType.cast(source));
}
/**
* Convert the supplied {@code source} object of type {@code S} into an object
* of type {@code T}.
*
* @param source the source object to convert; may be {@code null}
* @return the converted object; may be {@code null} but only if the target
* type is a reference type
* @throws ArgumentConversionException if an error occurs during the
* conversion
*/
protected abstract T convert(@Nullable S source) throws ArgumentConversionException;
}
View on GitHub (pinned to f070c699a0)