{"id":"aefdde5d16858664","repo":"junit-team/junit5","slug":"could-not-instantiate-timezoneprovider-because-of","errorCode":null,"errorMessage":"Could not instantiate TimeZoneProvider because of exception","messagePattern":"Could not instantiate TimeZoneProvider because of exception","errorType":"exception","errorClass":"ExtensionConfigurationException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultTimeZoneExtension.java","lineNumber":98,"sourceCode":"\t\tTimeZone configuredTimeZone = TimeZone.getTimeZone(timeZoneId);\n\t\t// TimeZone::getTimeZone returns with GMT as fallback if the given ID cannot be understood\n\t\tif (configuredTimeZone.equals(TimeZone.getTimeZone(\"GMT\")) && !\"GMT\".equals(timeZoneId)) {\n\t\t\tthrow new ExtensionConfigurationException(\"\"\"\n\t\t\t\t\t@DefaultTimeZone not configured correctly.\n\t\t\t\t\tCould not find the specified time zone + '%s'.\n\t\t\t\t\tPlease use correct identifiers, e.g. \"GMT\" for Greenwich Mean Time.\n\t\t\t\t\t\"\"\".formatted(timeZoneId));\n\t\t}\n\t\treturn configuredTimeZone;\n\t}\n\n\tprivate static TimeZone createTimeZoneFromProvider(Class<? extends TimeZoneProvider> providerClass) {\n\t\ttry {\n\t\t\tTimeZoneProvider provider = ReflectionSupport.newInstance(providerClass);\n\t\t\treturn Optional.ofNullable(provider.get()).orElse(TimeZone.getTimeZone(\"GMT\"));\n\t\t}\n\t\tcatch (Exception exception) {\n\t\t\tthrow new ExtensionConfigurationException(\"Could not instantiate TimeZoneProvider because of exception\",\n\t\t\t\texception);\n\t\t}\n\t}\n\n\t@Override\n\tpublic void afterEach(ExtensionContext context) {\n\t\tload(context, DEFAULT_KEY).ifPresent(TimeZone::setDefault);\n\t}\n\n\tprivate static void store(ExtensionContext context, String key, TimeZone value) {\n\t\tgetStore(context).put(key, value);\n\t}\n\n\tprivate static Optional<TimeZone> load(ExtensionContext context, String key) {\n\t\treturn Optional.ofNullable(getStore(context).get(key, TimeZone.class));\n\t}\n\n\tprivate static ExtensionContext.Store getStore(ExtensionContext context) {","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultTimeZoneExtension.java#L80-L116","documentation":"Thrown as ExtensionConfigurationException by DefaultTimeZoneExtension.createTimeZoneFromProvider when ReflectionSupport.newInstance(providerClass) fails while constructing the TimeZoneProvider declared in @DefaultTimeZone(timeZoneProvider = ...). The original exception is attached as the cause. The provider must have an accessible no-arg constructor and must not throw during construction.","triggerScenarios":"Setting timeZoneProvider to a class lacking a no-arg constructor, an abstract class or interface, a non-public class with an inaccessible constructor, or a class whose constructor throws.","commonSituations":"Provider class requiring configuration arguments; constructor performing I/O or env access that fails in the test environment; package-private provider used across packages.","solutions":["Give the TimeZoneProvider implementation a public no-arg constructor.","Make the class concrete and accessible (public, top-level or public static nested).","Move fallible initialization out of the constructor into the get() method.","Inspect the attached cause exception to pinpoint the construction failure."],"exampleFix":"// before\npublic class MyTimeZoneProvider implements TimeZoneProvider {\n    public MyTimeZoneProvider(Path config) { /* no no-arg ctor */ }\n    public TimeZone get() { return TimeZone.getTimeZone(\"GMT\"); }\n}\n\n// after\npublic class MyTimeZoneProvider implements TimeZoneProvider {\n    public MyTimeZoneProvider() {}\n    public TimeZone get() { return TimeZone.getTimeZone(\"GMT\"); }\n}","handlingStrategy":"validation","validationCode":"Class<? extends TimeZoneProvider> c = MyTimeZoneProvider.class;\nif (c.isInterface() || Modifier.isAbstract(c.getModifiers()) || !Modifier.isPublic(c.getModifiers())) {\n    throw new IllegalStateException(\"TimeZoneProvider must be a public concrete class\");\n}\ntry {\n    c.getDeclaredConstructor();\n} catch (NoSuchMethodException e) {\n    throw new IllegalStateException(\"TimeZoneProvider needs a public no-arg constructor\", e);\n}\nReflectionSupport.newInstance(c); // verify ctor does not throw","typeGuard":"static boolean isUsableProvider(Class<? extends TimeZoneProvider> 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":["TimeZoneProvider implementations must be public, concrete, with a public no-arg constructor.","Defer fallible work to get(); keep the constructor side-effect-free.","Unit-test provider construction before referencing it from @DefaultTimeZone."],"tags":["default-timezone","timezone-provider","reflection","extension-configuration","junit-jupiter"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}