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 ClassOrderer.Default, a sentinel/placeholder implementation introduced in JUnit 6.0. Its purpose is to reset the class ordering for nested classes, so it is referenced by type and never instantiated; attempting to reflectively or directly construct it fails fast with a JUnitException.

Solutions

  1. Never instantiate ClassOrderer.Default — reference it only as a type reference, e.g. @TestClassOrder(methodOrderers = ..., classOrderers = ...) where Default is used by class identity.
  2. If a factory must enumerate orderers, exclude ClassOrderer.Default (and MethodOrderer.Default) from instantiation and handle them as no-op sentinels.
  3. Use a different concrete ClassOrderer (ClassName, ClassDisplayName, etc.) when you actually need an instance to call orderClasses on.

Example fix

// before
ClassOrderer o = new ClassOrderer.Default();
// after
// reference by type only; to reset nested ordering, declare it in the annotation:
@TestClassOrder(classOrderers = ClassOrderer.Default.class)
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Calling new ClassOrderer.Default() directly, or using reflection (ReflectionSupport.newInstance, Constructor.newInstance) to instantiate ClassOrderer.Default. The private constructor is the only constructor and it unconditionally throws.

Common situations: A generic factory or test harness that instantiates every ClassOrderer implementation by reflection; copying a pattern that works for MethodName/String orderers onto Default; migration tooling that enumerates and constructs enum-like nested classes.

Related errors


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

Appendix: source

Thrown at junit-jupiter-api/src/main/java/org/junit/jupiter/api/ClassOrderer.java:123

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

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

		@Override
		public void orderClasses(ClassOrdererContext context) {
			// never called
		}
	}

	/**
	 * {@code ClassOrderer} that sorts classes alphanumerically based on their
	 * fully qualified names using {@link String#compareTo(String)}.
	 */
	class ClassName implements ClassOrderer {

		public ClassName() {
		}

		/**

View on GitHub (pinned to f070c699a0)