junit-team/junit5 · error · JUnitException

This class must not be instantiated

Error message

This class must not be instantiated

What it means

Thrown from the private constructor of MethodOrderer.Default, a sentinel/placeholder MethodOrderer introduced in JUnit 6.0 used to reset method ordering for nested classes. Like ClassOrderer.Default it is referenced by type only; constructing it fails fast with a JUnitException.

Solutions

  1. Never instantiate MethodOrderer.Default — reference it by type only, e.g. @TestMethodOrder with the Default sentinel declared by class identity.
  2. If a factory enumerates orderers, skip MethodOrderer.Default (and ClassOrderer.Default) and treat them as no-op sentinels.
  3. Use a concrete MethodOrderer (MethodName, OrderAnnotation, Random, etc.) when you need an instance to call orderMethods on.

Example fix

// before
MethodOrderer o = new MethodOrderer.Default();
// after
// reset nested method ordering by type reference only:
@TestMethodOrder(MethodOrderer.Default.class)
Defensive patterns

Strategy: type-guard

Validate before calling

if (ordererType == MethodOrderer.Default.class) {
    // reference only; do not instantiate
}

Type guard

static boolean isSentinelMethodOrderer(Class<? extends MethodOrderer> c) {
    return c == MethodOrderer.Default.class;
}

Prevention

When it happens

Trigger: Calling new MethodOrderer.Default() or constructing it via reflection (ReflectionSupport.newInstance / Constructor.newInstance). The sole private constructor always throws.

Common situations: Reflective factories that instantiate every MethodOrderer implementation; copying an instantiation pattern from MethodOrderer.MethodName onto Default; tooling that enumerates nested classes and constructs them.

Related errors


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

Appendix: source

Thrown at junit-jupiter-api/src/main/java/org/junit/jupiter/api/MethodOrderer.java:151

	 * ordering should be applied.
	 *
	 * <p>If the {@value #DEFAULT_ORDER_PROPERTY_NAME} is set, specifying this
	 * {@code MethodOrderer} has the same effect as referencing the configured
	 * class directly. Otherwise, it has the same effect as not specifying any
	 * {@code MethodOrderer}.
	 *
	 * <p>This class can be used to reset the {@code MethodOrderer} for a
	 * {@link Nested @Nested} class and its {@code @Nested} inner classes,
	 * recursively, when a {@code MethodOrderer} is configured using
	 * {@link TestMethodOrder @TestMethodOrder} on an enclosing class.
	 *
	 * @since 6.0
	 */
	@API(status = EXPERIMENTAL, since = "6.0")
	final class Default implements MethodOrderer {

		private Default() {
			throw new JUnitException("This class must not be instantiated");
		}

		@Override
		public void orderMethods(MethodOrdererContext context) {
			// never called
		}
	}

	/**
	 * {@code MethodOrderer} that sorts methods alphanumerically based on their
	 * names using {@link String#compareTo(String)}.
	 *
	 * <p>If two methods have the same name, {@code String} representations of
	 * their formal parameter lists will be used as a fallback for comparing the
	 * methods.
	 *
	 * @since 5.7
	 */

View on GitHub (pinned to f070c699a0)