{"record":{"id":"dc8eef820d00ea5d","repo":"testcontainers/testcontainers-java","slug":"can-t-be-reused-because-it-overrides","errorCode":null,"errorMessage":"{} can't be reused because it overrides {}","messagePattern":"(.+?) can't be reused because it overrides (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"core/src/main/java/org/testcontainers/containers/GenericContainer.java","lineNumber":357,"sourceCode":"                            startupAttempts\n                        );\n                    tryStart();\n                    return true;\n                }\n            );\n        } catch (Exception e) {\n            throw new ContainerLaunchException(\"Container startup failed for image \" + getDockerImageName(), e);\n        }\n    }\n\n    @UnstableAPI\n    @SneakyThrows\n    protected boolean canBeReused() {\n        for (Class<?> type = getClass(); type != GenericContainer.class; type = type.getSuperclass()) {\n            try {\n                Method method = type.getDeclaredMethod(\"containerIsCreated\", String.class);\n                if (method.getDeclaringClass() != GenericContainer.class) {\n                    logger().warn(\"{} can't be reused because it overrides {}\", getClass(), method.getName());\n                    return false;\n                }\n            } catch (NoSuchMethodException | NoClassDefFoundError e) {\n                // ignore\n            }\n        }\n\n        return true;\n    }\n\n    private void tryStart() {\n        try {\n            String dockerImageName = getDockerImageName();\n            logger().debug(\"Starting container: {}\", dockerImageName);\n\n            Instant startedAt = Instant.now();\n            logger().info(\"Creating container for image: {}\", dockerImageName);\n            CreateContainerCmd createCommand = dockerClient.createContainerCmd(dockerImageName);","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/core/src/main/java/org/testcontainers/containers/GenericContainer.java#L339-L375","documentation":"GenericContainer.canBeReused() checks that the container subclass does not override containerIsCreated(String); overriding hooks would not run when reusing an existing container, breaking behavior. If an override is found it logs a warning ('{} can't be reused because it overrides {}') and returns false, so reuse (withReuse(true)) is disabled for that class.","triggerScenarios":"Using .withReuse(true) on a container subclass that overrides containerIsCreated; the container silently falls back to creating a fresh instance per run.","commonSituations":"Custom container classes overriding containerIsCreated for extra setup; upgrading testcontainers while keeping legacy lifecycle overrides; confusion about why container reuse is not taking effect despite identical hashes.","solutions":["Remove the containerIsCreated override and move setup logic into an earlier lifecycle hook (e.g. configure())","Accept the warning — reuse is intentionally disabled for this subclass","Subclass differently: apply custom setup before container creation rather than after"],"exampleFix":"// before\n@Override\nprotected void containerIsCreated(String containerId) { setup(); }\n// after\n@Override\nprotected void configure() { /* setup expressed as config */ }","handlingStrategy":"validation","validationCode":"// detect reuse-incompatible subclasses early\nfor (Class<?> t = custom.getClass(); t != GenericContainer.class; t = t.getSuperclass()) {\n    try { t.getDeclaredMethod(\"containerIsCreated\", String.class);\n          throw new IllegalStateException(t + \" overrides containerIsCreated; reuse disabled\"); }\n    catch (NoSuchMethodException ignored) {}\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid overriding containerIsCreated in reusable container classes","Express custom setup via configure() instead of post-creation hooks","Check logs for the 'can't be reused because it overrides' warning when expecting reuse"],"tags":["container-reuse","lifecycle","generic-container"],"backgroundTag":"unsupported-operation","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}