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

  1. Make the parameter/field type match (or be a supertype of) the converter's declared targetType.
  2. Update the converter's targetType generic to the type actually required by the parameter.
  3. 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

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


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)