junit-team/junit5 · error · ExtensionConfigurationException
Could not instantiate TimeZoneProvider because of exception
Error message
Could not instantiate TimeZoneProvider because of exception
What it means
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.
Source
Thrown at junit-jupiter-api/src/main/java/org/junit/jupiter/api/util/DefaultTimeZoneExtension.java:98
TimeZone configuredTimeZone = TimeZone.getTimeZone(timeZoneId);
// TimeZone::getTimeZone returns with GMT as fallback if the given ID cannot be understood
if (configuredTimeZone.equals(TimeZone.getTimeZone("GMT")) && !"GMT".equals(timeZoneId)) {
throw new ExtensionConfigurationException("""
@DefaultTimeZone not configured correctly.
Could not find the specified time zone + '%s'.
Please use correct identifiers, e.g. "GMT" for Greenwich Mean Time.
""".formatted(timeZoneId));
}
return configuredTimeZone;
}
private static TimeZone createTimeZoneFromProvider(Class<? extends TimeZoneProvider> providerClass) {
try {
TimeZoneProvider provider = ReflectionSupport.newInstance(providerClass);
return Optional.ofNullable(provider.get()).orElse(TimeZone.getTimeZone("GMT"));
}
catch (Exception exception) {
throw new ExtensionConfigurationException("Could not instantiate TimeZoneProvider because of exception",
exception);
}
}
@Override
public void afterEach(ExtensionContext context) {
load(context, DEFAULT_KEY).ifPresent(TimeZone::setDefault);
}
private static void store(ExtensionContext context, String key, TimeZone value) {
getStore(context).put(key, value);
}
private static Optional<TimeZone> load(ExtensionContext context, String key) {
return Optional.ofNullable(getStore(context).get(key, TimeZone.class));
}
private static ExtensionContext.Store getStore(ExtensionContext context) {View on GitHub (pinned to 956246301e)
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.
Example fix
// before
public class MyTimeZoneProvider implements TimeZoneProvider {
public MyTimeZoneProvider(Path config) { /* no no-arg ctor */ }
public TimeZone get() { return TimeZone.getTimeZone("GMT"); }
}
// after
public class MyTimeZoneProvider implements TimeZoneProvider {
public MyTimeZoneProvider() {}
public TimeZone get() { return TimeZone.getTimeZone("GMT"); }
} Defensive patterns
Strategy: validation
Validate before calling
Class<? extends TimeZoneProvider> c = MyTimeZoneProvider.class;
if (c.isInterface() || Modifier.isAbstract(c.getModifiers()) || !Modifier.isPublic(c.getModifiers())) {
throw new IllegalStateException("TimeZoneProvider must be a public concrete class");
}
try {
c.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalStateException("TimeZoneProvider needs a public no-arg constructor", e);
}
ReflectionSupport.newInstance(c); // verify ctor does not throw Type guard
static boolean isUsableProvider(Class<? extends TimeZoneProvider> c) {
if (c.isInterface() || Modifier.isAbstract(c.getModifiers())) return false;
try { c.getDeclaredConstructor(); return true; }
catch (NoSuchMethodException e) { return false; }
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Provider class requiring configuration arguments; constructor performing I/O or env access that fails in the test environment; package-private provider used across packages.
Related errors
- LocaleProvider instance could not be constructed because of
- Either a valid time zone id or a TimeZoneProvider must be pr
- @DefaultTimeZone not configured correctly. Could not find th
- This class must not be instantiated
- This class must not be instantiated
AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04).
Data as JSON: /data/errors/aefdde5d16858664.json.
Report an issue: GitHub.