{"id":"eac84f3c5050f19f","repo":"junit-team/junit5","slug":"localeprovider-instance-could-not-be-constructed-b","errorCode":null,"errorMessage":"LocaleProvider instance could not be constructed because of an exception","messagePattern":"LocaleProvider instance could not be constructed because of an exception","errorType":"exception","errorClass":"ExtensionConfigurationException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultLocaleExtension.java","lineNumber":112,"sourceCode":"\t\t}\n\t\telse {\n\t\t\tthrow new ExtensionConfigurationException(\n\t\t\t\t\"@DefaultLocale not configured correctly. When not using a language tag, specify either\"\n\t\t\t\t\t\t+ \" language, or language and country, or language and country and variant.\");\n\t\t}\n\t}\n\n\tprivate static Locale getFromProvider(DefaultLocale annotation) {\n\t\tif (!annotation.country().isEmpty() || !annotation.variant().isEmpty())\n\t\t\tthrow new ExtensionConfigurationException(\n\t\t\t\t\"@DefaultLocale can only be used with a provider if value, language, country and variant are not set.\");\n\t\tvar providerClass = annotation.localeProvider();\n\t\tLocaleProvider provider;\n\t\ttry {\n\t\t\tprovider = ReflectionSupport.newInstance(providerClass);\n\t\t}\n\t\tcatch (Exception exception) {\n\t\t\tthrow new ExtensionConfigurationException(\n\t\t\t\t\"LocaleProvider instance could not be constructed because of an exception\", exception);\n\t\t}\n\t\treturn invoke(provider);\n\t}\n\n\t@SuppressWarnings(\"ConstantValue\")\n\tprivate static Locale invoke(LocaleProvider provider) {\n\t\tvar locale = provider.get();\n\t\tif (locale == null) {\n\t\t\tthrow new ExtensionConfigurationException(\"LocaleProvider instance returned with null\");\n\t\t}\n\t\treturn locale;\n\t}\n\n\t@Override\n\tpublic void afterEach(ExtensionContext context) {\n\t\tload(context, DEFAULT_KEY).ifPresent(Locale::setDefault);\n\t}","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultLocaleExtension.java#L94-L130","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the LocaleProvider implementation has a public no-arg constructor.","Make sure the class is concrete (not abstract, not an interface) and top-level or public-static-nested.","Move any failing initialization out of the constructor (do it lazily in get()) or wrap it so construction cannot throw.","Read the attached cause exception in the stack trace to identify the exact construction failure."],"exampleFix":"// before\npublic class MyLocaleProvider implements LocaleProvider {\n    public MyLocaleProvider(String config) { /* no no-arg ctor */ }\n    public Locale get() { return Locale.US; }\n}\n\n// after\npublic class MyLocaleProvider implements LocaleProvider {\n    public MyLocaleProvider() { /* no-arg, does not throw */ }\n    public Locale get() { return Locale.US; }\n}","handlingStrategy":"validation","validationCode":"Class<? extends LocaleProvider> c = MyLocaleProvider.class;\nint mods = c.getModifiers();\nif (c.isInterface() || Modifier.isAbstract(mods) || !Modifier.isPublic(mods)) {\n    throw new IllegalStateException(\"LocaleProvider must be a public concrete class\");\n}\ntry {\n    c.getDeclaredConstructor(); // must have a no-arg ctor\n} catch (NoSuchMethodException e) {\n    throw new IllegalStateException(\"LocaleProvider needs a public no-arg constructor\", e);\n}\n// optionally: instantiate once to verify ctor does not throw\nReflectionSupport.newInstance(c);","typeGuard":"static boolean isUsableProvider(Class<? extends LocaleProvider> c) {\n    if (c.isInterface() || Modifier.isAbstract(c.getModifiers())) return false;\n    try { c.getDeclaredConstructor(); return true; }\n    catch (NoSuchMethodException e) { return false; }\n}","tryCatchPattern":null,"preventionTips":["LocaleProvider implementations must be public, concrete, and expose a public no-arg constructor.","Do not perform fallible work (I/O, network) in the provider constructor — defer it to get().","Unit-test provider construction in isolation before referencing it in @DefaultLocale."],"tags":["default-locale","locale-provider","reflection","extension-configuration","junit-jupiter"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}