{"id":"ff4d97ac53def727","repo":"junit-team/junit5","slug":"object-stored-under-key-s-is-not-of-required-ty","errorCode":null,"errorMessage":"Object stored under key [%s] is not of required type [%s], but was [%s]: %s","messagePattern":"Object stored under key \\[(.+?)\\] is not of required type \\[(.+?)\\], but was \\[(.+?)\\]: (.+?)","errorType":"exception","errorClass":"NamespacedHierarchicalStoreException","httpStatus":null,"severity":"error","filePath":"junit-platform-engine/src/main/java/org/junit/platform/engine/support/store/NamespacedHierarchicalStore.java","lineNumber":493,"sourceCode":"\n\tprivate <T> @Nullable T castToRequiredType(Object key, @Nullable Object value, Class<T> requiredType) {\n\t\tPreconditions.notNull(requiredType, \"requiredType must not be null\");\n\t\tif (value == null) {\n\t\t\treturn null;\n\t\t}\n\t\treturn castNonNullToRequiredType(key, value, requiredType);\n\t}\n\n\t@SuppressWarnings(\"unchecked\")\n\tprivate <T, V> T castNonNullToRequiredType(Object key, V value, Class<T> requiredType) {\n\t\tif (isAssignableTo(value, requiredType)) {\n\t\t\tif (requiredType.isPrimitive()) {\n\t\t\t\treturn (T) requireNonNull(getWrapperType(requiredType)).cast(value);\n\t\t\t}\n\t\t\treturn requiredType.cast(value);\n\t\t}\n\t\t// else\n\t\tthrow new NamespacedHierarchicalStoreException(\n\t\t\t\"Object stored under key [%s] is not of required type [%s], but was [%s]: %s\".formatted(key,\n\t\t\t\trequiredType.getName(), value.getClass().getName(), value));\n\t}\n\n\tprivate void rejectIfClosed() {\n\t\tif (this.closed) {\n\t\t\tthrow new NamespacedHierarchicalStoreException(\n\t\t\t\t\"A NamespacedHierarchicalStore cannot be modified or queried after it has been closed\");\n\t\t}\n\t}\n\n\tprivate record CompositeKey<N>(N namespace, Object key) {\n\n\t\tCompositeKey {\n\t\t\tPreconditions.notNull(namespace, \"namespace must not be null\");\n\t\t\tPreconditions.notNull(key, \"key must not be null\");\n\t\t}\n","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-engine/src/main/java/org/junit/platform/engine/support/store/NamespacedHierarchicalStore.java#L475-L511","documentation":"Thrown by NamespacedHierarchicalStore.castNonNullToRequiredType (line 484-496) as a NamespacedHierarchicalStoreException when a stored value is not assignable to the requested requiredType. It names the key, the required type, the actual type, and the value's toString(). This is the ExtensionContext.Store type-mismatch error hit by Jupiter extensions and store users.","triggerScenarios":"Calling store.get(namespace, key, SomeType.class), getOrComputeIfAbsent(..., requiredType), computeIfAbsent(..., requiredType), or remove(..., requiredType) where the value previously stored under (namespace, key) is of an incompatible class (e.g. stored a String but requested Integer, or stored a List and requested a custom type).","commonSituations":"Two extensions using the same Store key for different value types; refactoring an extension to change the stored type without migrating existing keys; serializable/snapshot scenarios where the restored type differs; copy-paste of a Store key string across unrelated extensions.","solutions":["Namespace your Store keys (e.g. 'com.acme.MyExt.myResource') so unrelated extensions cannot collide on the same key.","Ensure the same key is always stored with the same type across all extensions in your codebase.","Call the untyped get(namespace, key) and instanceof-check before casting, rather than the typed overload, if type may vary.","Use computeIfAbsent with a consistent creator so the stored type is deterministic."],"exampleFix":"// before\nstore.put(MY_NS, \"config\", \"42\");\nInteger n = store.get(MY_NS, \"config\", Integer.class); // throws: actual String\n\n// after\nstore.put(MY_NS, \"config\", 42);\nInteger n = store.get(MY_NS, \"config\", Integer.class); // ok","handlingStrategy":"type-guard","validationCode":"Object raw = store.get(MY_NS, key);\nif (raw != null && !SomeType.class.isInstance(raw)) {\n    throw new IllegalStateException(\"stored value under \" + key + \" is \" + raw.getClass());\n}\nSomeType value = store.get(MY_NS, key, SomeType.class);","typeGuard":"static <T> Optional<T> getIfType(ExtensionContext.Store store, Object key, Class<T> type) {\n    Object v = store.get(key);\n    return type.isInstance(v) ? Optional.of(type.cast(v)) : Optional.empty();\n}","tryCatchPattern":"try {\n    return store.get(key, SomeType.class);\n} catch (NamespacedHierarchicalStoreException e) {\n    // message tells actual vs required type\n    log.warn(\"type mismatch in store for {}\", key, e);\n    return null;\n}","preventionTips":["Namespace Store keys uniquely per extension to prevent cross-extension type collisions.","Always store and retrieve a key with the same type.","Prefer the untyped get() + instanceof over the typed overload when type may vary."],"tags":["junit-platform","store","extension-context","type-mismatch","class-cast"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}