junit-team/junit5 · error · ConversionException

Failed to convert String

Error message

Failed to convert String "%s" to type %s

What it means

Generic wrapper thrown when a registered StringToObjectConverter.convert raises something other than a ConversionException (which is rethrown unchanged at line 130-132). The original exception is attached as the cause so the caller can see the real failure (NumberFormatException, DateTimeParseException, IllegalArgumentException, ...).

Solutions

  1. Inspect getCause() on the thrown ConversionException to find the real parse error
  2. Fix the offending input value in the argument source
  3. Add a custom ArgumentConverter via @ConvertWith that handles the input format
  4. Use @CsvFileSource with nullValues / ignoreLeadingAndTrailingWhitespace where appropriate

Example fix

// before
@ParameterizedTest
@ValueSource(strings = "12a")
void test(int n) { ... }

// after
@ParameterizedTest
@ValueSource(strings = "12")
void test(int n) { ... }
Defensive patterns

Strategy: try-catch

Try / catch

try {
    T value = ConversionSupport.convert(source, targetType, classLoader);
} catch (ConversionException e) {
    Throwable cause = e.getCause(); // the real parse failure (NumberFormatException, etc.)
    // handle/log/return default
}

Prevention

When it happens

Trigger: ConversionSupport.convert("abc", Integer.class, loader) where the numeric parser throws NumberFormatException; convert("2024-13-99", LocalDate.class, loader) where the date parser throws DateTimeParseException; convert("NOPE", MyEnum.class, loader) where the enum lookup fails.

Common situations: Malformed numbers in @CsvSource/@ValueSource mapped to numeric types; bad date/time patterns reaching java.time targets; invalid enum names; UUID strings with wrong format; any source value a built-in converter cannot parse.

Related errors


AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11). Data as JSON: /api/errors/6f1b4706168a0482. Report an issue: GitHub.

Appendix: source

Thrown at junit-platform-commons/src/main/java/org/junit/platform/commons/support/conversion/ConversionSupport.java:135

			return (T) source;
		}

		Class<?> targetTypeToUse = toWrapperType(targetType);
		Optional<StringToObjectConverter> converter = stringToObjectConverters.stream().filter(
			candidate -> candidate.canConvertTo(targetTypeToUse)).findFirst();
		if (converter.isPresent()) {
			try {
				ClassLoader classLoaderToUse = classLoader != null ? classLoader
						: ClassLoaderUtils.getDefaultClassLoader();
				return (T) converter.get().convert(source, targetTypeToUse, classLoaderToUse);
			}
			catch (Exception ex) {
				if (ex instanceof ConversionException conversionException) {
					// simply rethrow it
					throw conversionException;
				}
				// else
				throw new ConversionException(
					"Failed to convert String \"%s\" to type %s".formatted(source, targetType.getTypeName()), ex);
			}
		}

		throw new ConversionException(
			"No built-in converter for source type java.lang.String and target type " + targetType.getTypeName());
	}

	private static Class<?> toWrapperType(Class<?> targetType) {
		Class<?> wrapperType = getWrapperType(targetType);
		return wrapperType != null ? wrapperType : targetType;
	}

}

View on GitHub (pinned to f070c699a0)