junit-team/junit5 · error · ArgumentAccessException

Argument at index [%d] with value [%s] and type [%s] could n

Error message

Argument at index [%d] with value [%s] and type [%s] could not be converted or cast to type [%s].

What it means

Thrown by DefaultArgumentsAccessor.get(int index, Class<T> requiredType) when the argument at the given index cannot be converted to the requested type via DefaultArgumentConverter, or when the converted value cannot be cast to the required type. This is an ArgumentAccessException that wraps the underlying conversion/cast failure.

Source

Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java:81

	public @Nullable Object get(int index) {
		Preconditions.condition(index >= 0 && index < this.arguments.length,
			() -> "index must be >= 0 and < %d".formatted(this.arguments.length));
		return this.arguments[index];
	}

	@Override
	public <T> @Nullable T get(int index, Class<T> requiredType) {
		Preconditions.notNull(requiredType, "requiredType must not be null");
		Object value = get(index);
		try {
			Object convertedValue = converter.apply(value, requiredType);
			return requiredType.cast(convertedValue);
		}
		catch (Exception ex) {
			String message = "Argument at index [%d] with value [%s] and type [%s] could not be converted or cast to type [%s].".formatted(
				index, value, ClassUtils.nullSafeToString(value == null ? null : value.getClass()),
				requiredType.getName());
			throw new ArgumentAccessException(message, ex);
		}
	}

	@Override
	public @Nullable Character getCharacter(int index) {
		return get(index, Character.class);
	}

	@Override
	public @Nullable Boolean getBoolean(int index) {
		return get(index, Boolean.class);
	}

	@Override
	public @Nullable Byte getByte(int index) {
		return get(index, Byte.class);
	}

View on GitHub (pinned to 956246301e)

Solutions

  1. Inspect the error message which includes the actual value, its type, and the requested type
  2. Ensure the source data (CSV, method source) provides values compatible with the requested type at that index
  3. Use accessor.getString(index) for raw access and parse manually if the type is uncertain
  4. Add a @ConvertWith converter for complex type transformations

Example fix

// before
@ParameterizedTest
@CsvSource({ "abc, 2" })
void test(ArgumentsAccessor accessor) {
    int a = accessor.getInteger(0); // 'abc' cannot convert to int
}

// after
@ParameterizedTest
@CsvSource({ "1, 2" })
void test(ArgumentsAccessor accessor) {
    int a = accessor.getInteger(0);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Check argument type compatibility before calling accessor.get(index, type):
Object raw = accessor.get(index);
if (raw != null && !requiredType.isAssignableFrom(raw.getClass())
        && !(raw instanceof String)) {
    throw new IllegalStateException(
        "Argument at " + index + " is " + raw.getClass() + ", not convertible to " + requiredType);
}

Type guard

// Narrow the type before accessing:
Object raw = accessor.get(index);
if (raw instanceof Integer i) {
    int value = i;
} else {
    // handle non-integer case
}

Try / catch

try {
    Integer value = accessor.getInteger(0);
} catch (ArgumentAccessException e) {
    // handle: log, skip, or provide default
    throw new AssertionFailedError("Expected integer at index 0: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling accessor.get(index, SomeType.class) on an ArgumentsAccessor where the argument at that index is incompatible with SomeType — e.g., accessor.get(0, Integer.class) when the argument is a non-numeric string 'abc', or accessor.get(0, LocalDate.class) when the string doesn't parse as a date.

Common situations: Using an ArgumentsAccessor (via @AggregateWith or a parameter of type ArgumentsAccessor) to flexibly access arguments, but requesting a type that doesn't match the actual argument. CSV data where a column sometimes contains non-numeric or non-parseable values. Assuming a column is always a number when it sometimes contains sentinel strings.

Related errors


AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04). Data as JSON: /data/errors/3ccf4ba0490a5116.json. Report an issue: GitHub.