junit-team/junit5 · error · ArgumentConversionException
Cannot convert null to ${targetClass.getName()}; consider se
Error message
Cannot convert null to ${targetClass.getName()}; consider setting 'nullable = true' What it means
Thrown by JavaTimeArgumentConverter.convert() when the input is null and the @JavaTimeConversionPattern annotation does not have nullable = true set. This converter is used when a parameter is annotated with @JavaTimeConversionPattern to parse date/time values. Unlike the default converter, null is rejected unless explicitly opted in.
Source
Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/converter/JavaTimeArgumentConverter.java:62
queries.put(LocalDateTime.class, LocalDateTime::from);
queries.put(LocalTime.class, LocalTime::from);
queries.put(OffsetDateTime.class, OffsetDateTime::from);
queries.put(OffsetTime.class, OffsetTime::from);
queries.put(Year.class, Year::from);
queries.put(YearMonth.class, YearMonth::from);
queries.put(ZonedDateTime.class, ZonedDateTime::from);
TEMPORAL_QUERIES = Collections.unmodifiableMap(queries);
}
@Override
protected @Nullable Object convert(@Nullable Object input, Class<?> targetClass,
JavaTimeConversionPattern annotation) {
if (input == null) {
if (annotation.nullable()) {
return null;
}
throw new ArgumentConversionException(
"Cannot convert null to " + targetClass.getName() + "; consider setting 'nullable = true'");
}
TemporalQuery<?> temporalQuery = TEMPORAL_QUERIES.get(targetClass);
if (temporalQuery == null) {
throw new ArgumentConversionException("Cannot convert to " + targetClass.getName() + ": " + input);
}
String pattern = annotation.value();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return formatter.parse(input.toString(), temporalQuery);
}
}
View on GitHub (pinned to 956246301e)
Solutions
- Add nullable = true to the @JavaTimeConversionPattern annotation
- Remove @NullSource / @NullAndEmptySource if null values are not expected for this parameter
- Ensure CSV columns for date parameters are never empty
Example fix
// before
@ParameterizedTest
@NullSource
void test(@JavaTimeConversionPattern("yyyy-MM-dd") LocalDate date) { }
// after
@ParameterizedTest
@NullSource
void test(@JavaTimeConversionPattern(value = "yyyy-MM-dd", nullable = true) LocalDate date) { } Defensive patterns
Strategy: validation
Validate before calling
// Check the annotation before using null-providing sources:
JavaTimeConversionPattern ann = parameter.getAnnotation(JavaTimeConversionPattern.class);
if (ann != null && !ann.nullable() && usesNullSource) {
throw new IllegalStateException(
"Set nullable = true on @JavaTimeConversionPattern or remove @NullSource");
} Prevention
- Set nullable = true on @JavaTimeConversionPattern when the source may provide null
- Remove @NullSource / @NullAndEmptySource from tests with @JavaTimeConversionPattern parameters if null is not expected
- Ensure CSV date columns are never empty when nullable is false
When it happens
Trigger: A @ParameterizedTest parameter annotated with @JavaTimeConversionPattern("yyyy-MM-dd") receiving a null argument from @NullSource, @NullAndEmptySource, or an empty CSV field, without setting nullable = true on the annotation.
Common situations: Adding @NullSource to a parameterized test that uses @JavaTimeConversionPattern for date parsing, without enabling nullable. CSV sources with empty date columns where the converter receives null. Combining null-testing sources with temporal type parameters.
Related errors
- Cannot convert null to primitive value of type ${targetType.
- Cannot convert to ${targetClass.getName()}: ${input}
- No built-in converter for source type %s and target type %s
- %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/8c113c56f31781a3.json.
Report an issue: GitHub.