{"record":{"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/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultTimeZoneExtension.java#L80-L116","documentation":"ExtensionConfigurationException from DefaultTimeZoneExtension.createTimeZoneFromProvider, raised when the configured TimeZoneProvider class cannot be instantiated via ReflectionSupport.newInstance. Any exception during class loading or construction (no usable constructor, inaccessible class, constructor throwing) is wrapped and rethrown.","triggerScenarios":"Specifying @DefaultTimeZone(timeZoneProvider = MyProvider.class) where MyProvider lacks a public no-arg constructor, is non-public/abstract, or its constructor throws.","commonSituations":"Provider with only a parameterized constructor; non-static nested provider class; constructor doing I/O or env access that fails; provider not exported in a modular setup.","solutions":["Add a public no-argument constructor to the TimeZoneProvider class.","Make the class public and top-level (or public static nested).","Move logic that can fail out of the constructor into get().","Inspect the wrapped cause for the specific reflection error."],"exampleFix":"// before\nclass MyProvider implements TimeZoneProvider {\n    public MyProvider(Config c) { /* ... */ }\n    public TimeZone get() { return TimeZone.getTimeZone(\"UTC\"); }\n}\n// after\npublic class MyProvider implements TimeZoneProvider {\n    public MyProvider() {}\n    public TimeZone get() { return TimeZone.getTimeZone(\"UTC\"); }\n}","handlingStrategy":"validation","validationCode":"Class<? extends TimeZoneProvider> c = annotation.timeZoneProvider();\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 instantiableTimeZoneProvider(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 TimeZoneProvider classes a public no-arg constructor.","Keep classes public and accessible to reflection.","Keep risky logic out of the constructor."],"tags":["default-timezone","timezone-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"}