{"id":"3b3812b0f908f9eb","repo":"junit-team/junit5","slug":"emptysource-cannot-provide-an-empty-argument-s","errorCode":null,"errorMessage":"@EmptySource cannot provide an empty argument %s: [%s] is not a supported type.","messagePattern":"@EmptySource cannot provide an empty argument (.+?): \\[(.+?)\\] is not a supported type\\.","errorType":"exception","errorClass":"PreconditionViolationException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EmptyArgumentsProvider.java","lineNumber":117,"sourceCode":"\t\t}\n\t\tif (SortedMap.class.equals(parameterType)) {\n\t\t\treturn Stream.of(arguments(Collections.emptySortedMap()));\n\t\t}\n\t\tif (NavigableMap.class.equals(parameterType)) {\n\t\t\treturn Stream.of(arguments(Collections.emptyNavigableMap()));\n\t\t}\n\t\tif (Collection.class.isAssignableFrom(parameterType) || Map.class.isAssignableFrom(parameterType)) {\n\t\t\tOptional<Constructor<?>> defaultConstructor = getDefaultConstructor(parameterType);\n\t\t\tif (defaultConstructor.isPresent()) {\n\t\t\t\treturn Stream.of(arguments(newInstance(defaultConstructor.get())));\n\t\t\t}\n\t\t}\n\t\tif (parameterType.isArray()) {\n\t\t\tObject array = Array.newInstance(parameterType.getComponentType(), 0);\n\t\t\treturn Stream.of(arguments(array));\n\t\t}\n\t\t// else\n\t\tthrow new PreconditionViolationException(\n\t\t\t\"@EmptySource cannot provide an empty argument %s: [%s] is not a supported type.\".formatted(\n\t\t\t\terrorDetailsSupplier.get(), parameterType.getName()));\n\t}\n\n\tprivate static Optional<Constructor<?>> getDefaultConstructor(Class<?> clazz) {\n\t\ttry {\n\t\t\treturn Optional.of(clazz.getConstructor());\n\t\t}\n\t\tcatch (NoSuchMethodException e) {\n\t\t\treturn Optional.empty();\n\t\t}\n\t}\n\n\t/**\n\t * @since 6.1\n\t */\n\tprivate static class EmptyIterable<E> implements Iterable<E> {\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EmptyArgumentsProvider.java#L99-L135","documentation":"EmptyArgumentsProvider.provideEmptyArgument throws PreconditionViolationException when the parameter type is not one @EmptySource can make empty: String, List, Set, SortedSet, NavigableSet, Map, SortedMap, NavigableMap, Collection, Iterable, Iterator, ListIterator, arrays, or a Collection/Map subtype with a public no-arg constructor. Primitives and arbitrary objects are unsupported.","triggerScenarios":"Annotating a parameter of an unsupported type with @EmptySource, e.g. `@EmptySource int n`, `@EmptySource double d`, or `@EmptySource Person p` (Person has no no-arg ctor and is not a Collection/Map subtype).","commonSituations":"Assuming @EmptySource covers primitives (it does not); using it on a domain type; a Collection subtype whose only constructor takes arguments.","solutions":["Switch the parameter to a supported type (String, an array, or a Collection/Map subtype with a public no-arg constructor).","For primitives, use @ValueSource (e.g. @ValueSource(ints = {0})) instead of @EmptySource.","For custom types, supply an empty instance via a custom ArgumentsProvider or @MethodSource.","Add a public no-arg constructor if the parameter is a Collection/Map subtype you own."],"exampleFix":"// before\n@ParameterizedTest\n@EmptySource\nvoid test(int n) { }\n\n// after\n@ParameterizedTest\n@ValueSource(ints = { 0 })\nvoid test(int n) { }","handlingStrategy":"type-guard","validationCode":"// Reject @EmptySource on unsupported parameter types up front.\nClass<?>[] supported = { String.class, List.class, Set.class, Map.class,\n    Collection.class, Iterable.class, Iterator.class, ListIterator.class,\n    SortedSet.class, NavigableSet.class, SortedMap.class, NavigableMap.class };\nClass<?> pt = method.getParameterTypes()[0];\nboolean ok = pt.isArray() || java.util.Arrays.asList(supported).contains(pt)\n    || Collection.class.isAssignableFrom(pt) || Map.class.isAssignableFrom(pt);\nif (!ok) throw new IllegalArgumentException(\"@EmptySource not supported for \" + pt);","typeGuard":"// Type guard: only allow @EmptySource on empty-constructible types.\nstatic boolean isEmptyable(Class<?> t) {\n    return t == String.class || t.isArray()\n        || Collection.class.isAssignableFrom(t)\n        || Map.class.isAssignableFrom(t)\n        || Iterator.class.isAssignableFrom(t)\n        || Iterable.class.isAssignableFrom(t);\n}","tryCatchPattern":"try {\n    // run the parameterized test\n} catch (PreconditionViolationException e) {\n    if (e.getMessage().contains(\"not a supported type\")) {\n        // change the parameter type or switch from @EmptySource to @ValueSource/@MethodSource\n    } else throw e;\n}","preventionTips":["Remember @EmptySource covers String, collections, maps, and arrays - never primitives.","For primitives, use @ValueSource with a zero-equivalent.","Add a public no-arg constructor to Collection/Map subtypes you want to empty-construct."],"tags":["junit","jupiter-params","empty-source","parameterized-test","type-mismatch"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}