junit-team/junit5 · error · PreconditionViolationException

@EmptySource cannot provide an empty argument

Error message

@EmptySource cannot provide an empty argument %s: [%s] is not a supported type.

What it means

Thrown by EmptyArgumentsProvider when @EmptySource is applied to a parameter whose type is not supported for empty provisioning: it handles Collection/Map with a default constructor, arrays, and a few specific types above; anything else falls through to the final PreconditionViolationException. The message names the unsupported type and error details.

Solutions

  1. Change the parameter type to one @EmptySource supports (String, List, Set, Map, array, Optional, etc.).
  2. For custom types, supply empty values explicitly via @NullSource/@ValueSource/@MethodSource instead of @EmptySource.
  3. Remove @EmptySource from parameters of unsupported types.

Example fix

// before
@ParameterizedTest
@EmptySource
void test(MyPojo p) { }

// after
@ParameterizedTest
@NullAndEmptySource
void test(List<?> items) { }
Defensive patterns

Strategy: validation

Validate before calling

// Check that @EmptySource is only applied to supported types
Set<Class<?>> supported = Set.of(String.class, List.class, ArrayList.class, Set.class, HashSet.class, Map.class, HashMap.class);
if (!supported.contains(parameterType) && !parameterType.isArray())
    throw new IllegalArgumentException("@EmptySource not supported for " + parameterType);

Type guard

static boolean supportsEmptySource(Class<?> t) {
    return t == String.class || Collection.class.isAssignableFrom(t) || Map.class.isAssignableFrom(t) || t.isArray();
}

Prevention

When it happens

Trigger: Annotating a @ParameterizedTest parameter of an unsupported type (e.g. a custom POJO, an int primitive, a type with no default constructor that is not an array/String/etc.) with @EmptySource. The provider exhausts its type checks and reaches the else branch.

Common situations: Applying @EmptySource to a parameter whose type is neither a Collection, Map, array, String, nor other built-in empty-supported type. Using @EmptySource together with a custom type expecting the framework to figure out emptiness.

Related errors


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

Appendix: source

Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EmptyArgumentsProvider.java:117

		}
		if (SortedMap.class.equals(parameterType)) {
			return Stream.of(arguments(Collections.emptySortedMap()));
		}
		if (NavigableMap.class.equals(parameterType)) {
			return Stream.of(arguments(Collections.emptyNavigableMap()));
		}
		if (Collection.class.isAssignableFrom(parameterType) || Map.class.isAssignableFrom(parameterType)) {
			Optional<Constructor<?>> defaultConstructor = getDefaultConstructor(parameterType);
			if (defaultConstructor.isPresent()) {
				return Stream.of(arguments(newInstance(defaultConstructor.get())));
			}
		}
		if (parameterType.isArray()) {
			Object array = Array.newInstance(parameterType.getComponentType(), 0);
			return Stream.of(arguments(array));
		}
		// else
		throw new PreconditionViolationException(
			"@EmptySource cannot provide an empty argument %s: [%s] is not a supported type.".formatted(
				errorDetailsSupplier.get(), parameterType.getName()));
	}

	private static Optional<Constructor<?>> getDefaultConstructor(Class<?> clazz) {
		try {
			return Optional.of(clazz.getConstructor());
		}
		catch (NoSuchMethodException e) {
			return Optional.empty();
		}
	}

	/**
	 * @since 6.1
	 */
	private static class EmptyIterable<E> implements Iterable<E> {

View on GitHub (pinned to f070c699a0)