{"id":"c890217f133d30f9","repo":"junit-team/junit5","slug":"this-class-must-not-be-instantiated-c89021","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/MethodOrderer.java","lineNumber":151,"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 MethodOrderer} 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 MethodOrderer}.\n\t *\n\t * <p>This class can be used to reset the {@code MethodOrderer} for a\n\t * {@link Nested @Nested} class and its {@code @Nested} inner classes,\n\t * recursively, when a {@code MethodOrderer} is configured using\n\t * {@link TestMethodOrder @TestMethodOrder} 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 MethodOrderer {\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 orderMethods(MethodOrdererContext context) {\n\t\t\t// never called\n\t\t}\n\t}\n\n\t/**\n\t * {@code MethodOrderer} that sorts methods alphanumerically based on their\n\t * names using {@link String#compareTo(String)}.\n\t *\n\t * <p>If two methods have the same name, {@code String} representations of\n\t * their formal parameter lists will be used as a fallback for comparing the\n\t * methods.\n\t *\n\t * @since 5.7\n\t */","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-api/src/main/java/org/junit/jupiter/api/MethodOrderer.java#L133-L169","documentation":"Thrown as a JUnitException by the private constructor of the inner sentinel class MethodOrderer.Default. Like ClassOrderer.Default, this class exists only to be referenced by Class literal in @TestMethodOrder(MethodOrderer.Default.class) to mean 'use the configured default method orderer'; it is a marker and must never be instantiated.","triggerScenarios":"Reflective instantiation of MethodOrderer.Default (ReflectionSupport.newInstance / Constructor.newInstance after setAccessible). Normal @TestMethodOrder usage references only the .class literal.","commonSituations":"Generic reflective factory code that tries to instantiate every MethodOrderer subtype found on the classpath; copying an instantiation pattern from a real orderer (MethodName, OrderAnnotation, Random) onto the Default marker.","solutions":["Reference by literal: @TestMethodOrder(MethodOrderer.Default.class); never call new MethodOrderer.Default().","When scanning for MethodOrderer subtypes, skip classes named 'Default' or with a private constructor.","Pass a supplier/factory rather than reflectively instantiating arbitrary subtypes."],"exampleFix":"// before\nMethodOrderer o = ReflectionSupport.newInstance(MethodOrderer.Default.class);\n\n// after\n@TestMethodOrder(MethodOrderer.Default.class)\nclass MyTests {}","handlingStrategy":"validation","validationCode":"Class<?> candidate = /* scanned MethodOrderer */;\nif (candidate == org.junit.jupiter.api.MethodOrderer.Default.class) {\n    return; // sentinel, do not instantiate\n}","typeGuard":"static boolean isInstantiableOrderer(Class<?> c) {\n    return MethodOrderer.class.isAssignableFrom(c)\n        && c != MethodOrderer.Default.class\n        && !Modifier.isAbstract(c.getModifiers());\n}","tryCatchPattern":null,"preventionTips":["Never instantiate MethodOrderer.Default; reference it by .class literal only.","Skip classes with private constructors in reflective orderer factories.","Use @TestMethodOrder(MethodOrderer.Default.class) to opt into the configured default."],"tags":["method-orderer","sentinel-class","reflection","junit-jupiter"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}