{"id":"2fb32aa27b84d0d4","repo":"junit-team/junit5","slug":"localeprovider-instance-returned-with-null","errorCode":null,"errorMessage":"LocaleProvider instance returned with null","messagePattern":"LocaleProvider instance returned with null","errorType":"exception","errorClass":"ExtensionConfigurationException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultLocaleExtension.java","lineNumber":122,"sourceCode":"\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}\n\n\tprivate static void store(ExtensionContext context, String key, Locale value) {\n\t\tgetStore(context).put(key, value);\n\t}\n\n\tprivate static Optional<Locale> load(ExtensionContext context, String key) {\n\t\treturn Optional.ofNullable(getStore(context).get(key, Locale.class));\n\t}\n\n\tprivate static ExtensionContext.Store getStore(ExtensionContext context) {","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultLocaleExtension.java#L104-L140","documentation":"Thrown as ExtensionConfigurationException by DefaultLocaleExtension.invoke when a successfully constructed LocaleProvider.get() returns null. The contract of LocaleProvider is to return a non-null Locale; null is treated as a programming error in the provider implementation.","triggerScenarios":"A LocaleProvider whose get() method returns null — e.g., it looks up a Locale from a map/env var that is absent and returns null instead of a default.","commonSituations":"Provider implementations that delegate to Optional.orElse(null) or Map.get on a possibly-absent key; providers that were not unit-tested for the absent-input case.","solutions":["Make get() never return null — fall back to Locale.getDefault() or a explicit default Locale.","Use Optional.map(...).orElse(Locale.ROOT) instead of returning null.","Add a unit test that invokes get() in the no-input state to catch null returns early."],"exampleFix":"// before\npublic Locale get() {\n    return System.getenv(\"FORCE_LOCALE\") == null\n        ? null\n        : Locale.forLanguageTag(System.getenv(\"FORCE_LOCALE\"));\n}\n\n// after\npublic Locale get() {\n    String tag = System.getenv(\"FORCE_LOCALE\");\n    return tag != null ? Locale.forLanguageTag(tag) : Locale.ROOT;\n}","handlingStrategy":"validation","validationCode":"LocaleProvider provider = ReflectionSupport.newInstance(MyLocaleProvider.class);\nLocale result = provider.get();\njava.util.Objects.requireNonNull(result, \"LocaleProvider.get() must never return null\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["LocaleProvider.get() must never return null — fall back to Locale.ROOT or Locale.getDefault().","Use Optional.map(...).orElse(Locale.ROOT) instead of returning null from a lookup.","Add a unit test invoking get() with no input to catch null returns."],"tags":["default-locale","locale-provider","null-contract","extension-configuration","junit-jupiter"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}