junit-team/junit5 · error · JUnitException

Must not be instantiated

Error message

Must not be instantiated

What it means

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.

Source

Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EmptyArgumentsProvider.java:218

		@Override
		public int previousIndex() {
			return -1;
		}

		@Override
		public void set(E e) {
			throw new UnsupportedOperationException();
		}

		@Override
		public void add(E e) {
			throw new UnsupportedOperationException();
		}
	}

	static final class Derived {
		private Derived() {
			throw new JUnitException("Must not be instantiated");
		}
	}
}

View on GitHub (pinned to 956246301e)

Solutions

  1. Never instantiate EmptySource's Derived sentinel - leave @EmptySource.type() at its default.
  2. If you need a concrete empty value of a specific type, set @EmptySource(type = ThatType.class) where ThatType is supported.
  3. Exclude annotation-default types from reflection-based instantiation utilities.

Example fix

// before
new EmptySource.Derived();   // or reflection.newInstance on Derived.class

// after
// remove the instantiation; use @EmptySource without `type`
Defensive patterns

Strategy: validation

Validate before calling

// Forbid instantiating the Derived sentinel in reflection utilities.
if (EmptySource.class.isAnnotationPresent(
        java.lang.annotation.Annotation.class)) {
    Class<?> typeDefault = EmptySource.class.getMethod("type").getDefaultValue().getClass();
    if (typeDefault == EmptyArgumentsProvider.Derived.class) {
        // do not instantiate; treat Derived as 'derive from parameter'
    }
}

Try / catch

try {
    // code that may instantiate Derived via reflection
} catch (JUnitException e) {
    if (e.getMessage().equals("Must not be instantiated")) {
        // skip the sentinel; it only marks 'derive type from parameter'
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Reflection-based test harnesses that instantiate annotation default values; copy-paste of the sentinel into user code; misuse of @EmptySource.type() with Derived explicitly.

Related errors


AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04). Data as JSON: /data/errors/8fcf1e13febe9684.json. Report an issue: GitHub.