junit-team/junit5 · error · ArgumentConversionException

Cannot convert null to primitive value of type ${targetType.

Error message

Cannot convert null to primitive value of type ${targetType.getTypeName()}

What it means

Thrown by DefaultArgumentConverter.convert() when the source argument is null and the target parameter type is a primitive (int, long, boolean, etc.). Primitive types cannot hold null values, so the conversion fails. For reference types (Integer, Long, etc.), null is passed through without error.

Source

Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/DefaultArgumentConverter.java:78

	public final @Nullable Object convert(@Nullable Object source, ParameterContext context) {
		Class<?> targetType = context.getParameter().getType();
		ClassLoader classLoader = getClassLoader(context.getDeclaringExecutable().getDeclaringClass());
		return convert(source, targetType, classLoader);
	}

	@Override
	public final @Nullable Object convert(@Nullable Object source, FieldContext context)
			throws ArgumentConversionException {

		Class<?> targetType = context.getField().getType();
		ClassLoader classLoader = getClassLoader(context.getField().getDeclaringClass());
		return convert(source, targetType, classLoader);
	}

	public final @Nullable Object convert(@Nullable Object source, Class<?> targetType, ClassLoader classLoader) {
		if (source == null) {
			if (targetType.isPrimitive()) {
				throw new ArgumentConversionException(
					"Cannot convert null to primitive value of type " + targetType.getTypeName());
			}
			return null;
		}

		if (ReflectionUtils.isAssignableTo(source, targetType)) {
			return source;
		}

		if (source instanceof String string) {
			try {
				return convert(string, targetType, classLoader);
			}
			catch (ConversionException ex) {
				throw new ArgumentConversionException(ex.getMessage(), ex);
			}
		}

View on GitHub (pinned to 956246301e)

Solutions

  1. Change the primitive parameter to its wrapper type (int -> Integer, boolean -> Boolean) so null is accepted
  2. Remove @NullSource / @NullAndEmptySource if the parameter must be primitive
  3. For CSV sources, ensure no column is empty for primitive parameters, or configure nullValues appropriately
  4. Ensure @MethodSource methods never return null elements in the stream

Example fix

// before
@ParameterizedTest
@NullSource
void test(int value) { } // int cannot be null

// after
@ParameterizedTest
@NullSource
void test(Integer value) { } // Integer accepts null
Defensive patterns

Strategy: validation

Validate before calling

// Before using null-providing sources, check parameter types:
for (java.lang.reflect.Parameter p : method.getParameters()) {
    if (p.getType().isPrimitive()) {
        // Cannot use @NullSource / @NullAndEmptySource with this parameter
        System.err.println("Primitive param " + p + " cannot receive null");
    }
}

Type guard

// Ensure parameter is a wrapper type, not primitive:
// int  -> Integer
// long -> Long
// boolean -> Boolean
// etc.

Prevention

When it happens

Trigger: A @ParameterizedTest method declaring a primitive parameter (e.g., int, boolean) receiving a null argument from @NullSource, @NullAndEmptySource, @CsvSource with an empty field (when nullValues are configured), or a @MethodSource returning null. The DefaultArgumentConverter cannot assign null to a primitive.

Common situations: Using @NullSource or @NullAndEmptySource with a test that declares primitive parameters. CSV sources where a column is blank and configured to treat blanks as null. A @MethodSource that accidentally returns null in its stream. Parameter types that are primitives but the test data includes null sentinel values.

Related errors


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