junit-team/junit5 · error · ExtensionConfigurationException

LocaleProvider instance could not be constructed because of

Error message

LocaleProvider instance could not be constructed because of an exception

What it means

Thrown as ExtensionConfigurationException by DefaultLocaleExtension.getFromProvider when ReflectionSupport.newInstance(providerClass) fails while constructing the LocaleProvider declared in @DefaultLocale(localeProvider = ...). The original exception is attached as the cause. The provider class must have an accessible no-arg constructor and must not throw during construction.

Source

Thrown at junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultLocaleExtension.java:112

		}
		else {
			throw new ExtensionConfigurationException(
				"@DefaultLocale not configured correctly. When not using a language tag, specify either"
						+ " language, or language and country, or language and country and variant.");
		}
	}

	private static Locale getFromProvider(DefaultLocale annotation) {
		if (!annotation.country().isEmpty() || !annotation.variant().isEmpty())
			throw new ExtensionConfigurationException(
				"@DefaultLocale can only be used with a provider if value, language, country and variant are not set.");
		var providerClass = annotation.localeProvider();
		LocaleProvider provider;
		try {
			provider = ReflectionSupport.newInstance(providerClass);
		}
		catch (Exception exception) {
			throw new ExtensionConfigurationException(
				"LocaleProvider instance could not be constructed because of an exception", exception);
		}
		return invoke(provider);
	}

	@SuppressWarnings("ConstantValue")
	private static Locale invoke(LocaleProvider provider) {
		var locale = provider.get();
		if (locale == null) {
			throw new ExtensionConfigurationException("LocaleProvider instance returned with null");
		}
		return locale;
	}

	@Override
	public void afterEach(ExtensionContext context) {
		load(context, DEFAULT_KEY).ifPresent(Locale::setDefault);
	}

View on GitHub (pinned to 956246301e)

Solutions

  1. Ensure the LocaleProvider implementation has a public no-arg constructor.
  2. Make sure the class is concrete (not abstract, not an interface) and top-level or public-static-nested.
  3. Move any failing initialization out of the constructor (do it lazily in get()) or wrap it so construction cannot throw.
  4. Read the attached cause exception in the stack trace to identify the exact construction failure.

Example fix

// before
public class MyLocaleProvider implements LocaleProvider {
    public MyLocaleProvider(String config) { /* no no-arg ctor */ }
    public Locale get() { return Locale.US; }
}

// after
public class MyLocaleProvider implements LocaleProvider {
    public MyLocaleProvider() { /* no-arg, does not throw */ }
    public Locale get() { return Locale.US; }
}
Defensive patterns

Strategy: validation

Validate before calling

Class<? extends LocaleProvider> c = MyLocaleProvider.class;
int mods = c.getModifiers();
if (c.isInterface() || Modifier.isAbstract(mods) || !Modifier.isPublic(mods)) {
    throw new IllegalStateException("LocaleProvider must be a public concrete class");
}
try {
    c.getDeclaredConstructor(); // must have a no-arg ctor
} catch (NoSuchMethodException e) {
    throw new IllegalStateException("LocaleProvider needs a public no-arg constructor", e);
}
// optionally: instantiate once to verify ctor does not throw
ReflectionSupport.newInstance(c);

Type guard

static boolean isUsableProvider(Class<? extends LocaleProvider> c) {
    if (c.isInterface() || Modifier.isAbstract(c.getModifiers())) return false;
    try { c.getDeclaredConstructor(); return true; }
    catch (NoSuchMethodException e) { return false; }
}

Prevention

When it happens

Trigger: Setting localeProvider to a class with no no-arg constructor, an abstract class, an interface, a non-public class with an inaccessible constructor, or a class whose constructor throws an exception.

Common situations: Provider class designed to be constructed with arguments but referenced as a no-arg provider; provider constructor that does I/O or reads env state and fails in the test environment; package-private provider class used from a different package.

Related errors


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