{"record":{"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/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultLocaleExtension.java#L94-L130","documentation":"ExtensionConfigurationException from DefaultLocaleExtension.getFromProvider, thrown when the configured LocaleProvider class cannot be instantiated. The class is loaded and constructed via ReflectionSupport.newInstance; any exception (NoSuchMethodException, SecurityException, InvocationTargetException from a failing constructor, IllegalAccessException) is wrapped and rethrown.","triggerScenarios":"Specifying @DefaultLocale(localeProvider = MyProvider.class) where MyProvider has no public no-arg constructor, is abstract, is non-public/inaccessible, or its constructor throws.","commonSituations":"Provider class with only a parameterized constructor; a nested non-static provider class; provider constructor doing work that fails (reading a file, env lookup); provider not on the classpath module path in a modular test run.","solutions":["Give the LocaleProvider class a public no-argument constructor.","Make the class public (and top-level or public static nested) so reflection can reach it.","Ensure the provider constructor does not throw — move risky initialization into get().","Inspect the wrapped cause exception for the precise reflection failure."],"exampleFix":"// before\nclass MyProvider implements LocaleProvider {\n    public MyProvider(String cfg) { /* ... */ }\n    public Locale get() { return Locale.US; }\n}\n// after\npublic class MyProvider implements LocaleProvider {\n    public MyProvider() {}\n    public Locale get() { return Locale.US; }\n}","handlingStrategy":"validation","validationCode":"Class<? extends LocaleProvider> c = annotation.localeProvider();\nint mods = c.getModifiers();\nif (Modifier.isAbstract(mods) || !Modifier.isPublic(mods)) { /* fix visibility */ }\nif (Arrays.stream(c.getConstructors()).noneMatch(con -> con.getParameterCount() == 0)) { /* add no-arg ctor */ }","typeGuard":"static boolean instantiableLocaleProvider(Class<?> c) {\n    int m = c.getModifiers();\n    return Modifier.isPublic(m) && !Modifier.isAbstract(m)\n        && Arrays.stream(c.getConstructors()).anyMatch(con -> con.getParameterCount() == 0);\n}","tryCatchPattern":null,"preventionTips":["Give LocaleProvider classes a public no-arg constructor.","Keep provider classes public and top-level or public static nested.","Keep constructors side-effect free; defer logic to get()."],"tags":["default-locale","locale-provider","reflection","instantiation","extension-config"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}