{"id":"1f2a0a0a728ebf10","repo":"junit-team/junit5","slug":"this-class-must-not-be-instantiated","errorCode":null,"errorMessage":"This class must not be instantiated","messagePattern":"This class must not be instantiated","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-api/src/main/java/org/junit/jupiter/api/ClassOrderer.java","lineNumber":123,"sourceCode":"\t * ordering should be applied.\n\t *\n\t * <p>If the {@value #DEFAULT_ORDER_PROPERTY_NAME} is set, specifying this\n\t * {@code ClassOrderer} has the same effect as referencing the configured\n\t * class directly. Otherwise, it has the same effect as not specifying any\n\t * {@code ClassOrderer}.\n\t *\n\t * <p>This class can be used to reset the {@code ClassOrderer} for a\n\t * {@link Nested @Nested} class and its {@code @Nested} inner classes,\n\t * recursively, when a {@code ClassOrderer} is configured using\n\t * {@link TestClassOrder @TestClassOrder} on an enclosing class.\n\t *\n\t * @since 6.0\n\t */\n\t@API(status = EXPERIMENTAL, since = \"6.0\")\n\tfinal class Default implements ClassOrderer {\n\n\t\tprivate Default() {\n\t\t\tthrow new JUnitException(\"This class must not be instantiated\");\n\t\t}\n\n\t\t@Override\n\t\tpublic void orderClasses(ClassOrdererContext context) {\n\t\t\t// never called\n\t\t}\n\t}\n\n\t/**\n\t * {@code ClassOrderer} that sorts classes alphanumerically based on their\n\t * fully qualified names using {@link String#compareTo(String)}.\n\t */\n\tclass ClassName implements ClassOrderer {\n\n\t\tpublic ClassName() {\n\t\t}\n\n\t\t/**","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-api/src/main/java/org/junit/jupiter/api/ClassOrderer.java#L105-L141","documentation":"Thrown as a JUnitException by the private constructor of the inner sentinel class ClassOrderer.Default. The class exists only to be referenced by its Class literal (e.g. @TestClassOrder(ClassOrderer.Default.class)) to signal 'use the configured default orderer'; it is a marker and must never be instantiated. The constructor throws to fail-fast on accidental instantiation.","triggerScenarios":"Using reflection to instantiate ClassOrderer.Default via ReflectionSupport.newInstance, Constructor.newInstance, or Class.getDeclaredConstructor().newInstance() (the private constructor is made accessible). Normal @TestClassOrder usage only references the .class literal and never triggers this.","commonSituations":"Test frameworks or generic factory code that reflectively instantiates any ClassOrderer implementation by scanning the classpath; copy-paste of an instantiation pattern from a real orderer (ClassName, OrderAnnotation) onto the Default marker.","solutions":["Reference the class by literal only: use @TestClassOrder(ClassOrderer.Default.class), never new ClassOrderer.Default().","If scanning for ClassOrderer subtypes, exclude sentinel classes whose simple name is 'Default' (or check Modifier.isPrivate(constructor) and skip).","Refactor generic instantiation code to take a no-arg factory / supplier instead of reflectively newInstance-ing arbitrary subclasses."],"exampleFix":"// before\nClassOrderer o = ReflectionSupport.newInstance(ClassOrderer.Default.class);\n\n// after — never instantiate; pass the class literal to the annotation\n@TestClassOrder(ClassOrderer.Default.class)\nclass MyTests {}","handlingStrategy":"validation","validationCode":"// Before reflectively instantiating a ClassOrderer, skip sentinels\nClass<?> clazz = candidate;\nboolean isSentinel = clazz == org.junit.jupiter.api.ClassOrderer.Default.class;\nif (isSentinel) {\n    return; // never instantiate\n}","typeGuard":"static boolean isInstantiableOrderer(Class<?> c) {\n    return ClassOrderer.class.isAssignableFrom(c)\n        && c != ClassOrderer.Default.class\n        && !Modifier.isAbstract(c.getModifiers());\n}","tryCatchPattern":null,"preventionTips":["Never call new on inner classes named 'Default' in JUnit (ClassOrderer.Default, MethodOrderer.Default) — they are markers.","Reference orderers by Class literal in @TestClassOrder / @TestMethodOrder only.","When writing reflective factory code, skip classes with private constructors."],"tags":["class-orderer","sentinel-class","reflection","junit-jupiter"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}