{"id":"8fcf1e13febe9684","repo":"junit-team/junit5","slug":"must-not-be-instantiated","errorCode":null,"errorMessage":"Must not be instantiated","messagePattern":"Must not be instantiated","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EmptyArgumentsProvider.java","lineNumber":218,"sourceCode":"\t\t@Override\n\t\tpublic int previousIndex() {\n\t\t\treturn -1;\n\t\t}\n\n\t\t@Override\n\t\tpublic void set(E e) {\n\t\t\tthrow new UnsupportedOperationException();\n\t\t}\n\n\t\t@Override\n\t\tpublic void add(E e) {\n\t\t\tthrow new UnsupportedOperationException();\n\t\t}\n\t}\n\n\tstatic final class Derived {\n\t\tprivate Derived() {\n\t\t\tthrow new JUnitException(\"Must not be instantiated\");\n\t\t}\n\t}\n}\n","sourceCodeStart":200,"sourceCodeEnd":222,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EmptyArgumentsProvider.java#L200-L222","documentation":"EmptyArgumentsProvider.Derived is an internal sentinel used as the default for @EmptySource.type(), meaning 'derive the type from the parameter'. Its private constructor unconditionally throws JUnitException to forbid instantiation. This error only appears if something tries to construct Derived directly.","triggerScenarios":"Calling `new EmptyArgumentsProvider.Derived()` (if accessible) or instantiating it via reflection / Class.newInstance; reflection-driven utilities that walk annotation defaults and try to instantiate each type.","commonSituations":"Reflection-based test harnesses that instantiate annotation default values; copy-paste of the sentinel into user code; misuse of @EmptySource.type() with Derived explicitly.","solutions":["Never instantiate EmptySource's Derived sentinel - leave @EmptySource.type() at its default.","If you need a concrete empty value of a specific type, set @EmptySource(type = ThatType.class) where ThatType is supported.","Exclude annotation-default types from reflection-based instantiation utilities."],"exampleFix":"// before\nnew EmptySource.Derived();   // or reflection.newInstance on Derived.class\n\n// after\n// remove the instantiation; use @EmptySource without `type`","handlingStrategy":"validation","validationCode":"// Forbid instantiating the Derived sentinel in reflection utilities.\nif (EmptySource.class.isAnnotationPresent(\n        java.lang.annotation.Annotation.class)) {\n    Class<?> typeDefault = EmptySource.class.getMethod(\"type\").getDefaultValue().getClass();\n    if (typeDefault == EmptyArgumentsProvider.Derived.class) {\n        // do not instantiate; treat Derived as 'derive from parameter'\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    // code that may instantiate Derived via reflection\n} catch (JUnitException e) {\n    if (e.getMessage().equals(\"Must not be instantiated\")) {\n        // skip the sentinel; it only marks 'derive type from parameter'\n    } else throw e;\n}","preventionTips":["Treat EmptySource.Derived as a marker, never instantiate it.","Exclude annotation-default types from reflection-based object factories.","Leave @EmptySource.type() at its default unless you have a concrete supported type."],"tags":["junit","jupiter-params","empty-source","internal-api","misuse"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}