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
- Change the primitive parameter to its wrapper type (int -> Integer, boolean -> Boolean) so null is accepted
- Remove @NullSource / @NullAndEmptySource if the parameter must be primitive
- For CSV sources, ensure no column is empty for primitive parameters, or configure nullValues appropriately
- 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
- Use wrapper types (Integer, Long, Boolean) instead of primitives when testing with @NullSource
- Remove @NullSource from tests with primitive parameters
- For CSV sources, ensure no column is empty for primitive parameters
- Ensure @MethodSource streams never contain null elements for primitive parameters
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
- Cannot convert null to ${targetClass.getName()}; consider se
- No built-in converter for source type %s and target type %s
- Cannot convert to ${targetClass.getName()}: ${input}
- %s cannot convert objects of type [%s]. Only source objects
- Unsupported argument count validation mode: <mode>
AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04).
Data as JSON: /data/errors/ddfcda46ebf69ae5.json.
Report an issue: GitHub.