{"record":{"id":"8228907104bdd827","repo":"testcontainers/testcontainers-java","slug":"setter-can-only-be-called-before-the-container-is-running","errorCode":null,"errorMessage":"Setter can only be called before the container is running","messagePattern":"Setter can only be called before the container is running","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java","lineNumber":803,"sourceCode":"            String body = null;\n            if (response.body() != null) {\n                try {\n                    body = response.body().string();\n                } catch (IOException e) {\n                    logger().debug(\"Unable to read body of response: {}\", response, e);\n                }\n            }\n\n            throw new IllegalStateException(message + \": \" + response + \", body=\" + (body == null ? \"<null>\" : body));\n        }\n    }\n\n    /**\n     * Checks if already running and if so raises an exception to prevent too-late setters.\n     */\n    private void checkNotRunning() {\n        if (isRunning()) {\n            throw new IllegalStateException(\"Setter can only be called before the container is running\");\n        }\n    }\n\n    /**\n     * Helper method to perform a request against a couchbase server HTTP endpoint.\n     *\n     * @param port the (unmapped) original port that should be used.\n     * @param path the relative http path.\n     * @param method the http method to use.\n     * @param body if present, will be part of the payload.\n     * @param auth if authentication with the admin user and password should be used.\n     * @return the response of the request.\n     */\n    private Response doHttpRequest(\n        final int port,\n        final String path,\n        final String method,\n        final RequestBody body,","sourceCodeStart":785,"sourceCodeEnd":821,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java#L785-L821","documentation":"CouchbaseContainer throws this IllegalStateException when a configuration setter (bucket, credentials, services, quotas) is invoked after the container has already started. These settings must be applied while the container is stopped so they can be used to bootstrap the Couchbase node. Once isRunning() returns true, late mutation is rejected to avoid inconsistent server state.","triggerScenarios":"Calling withCredentials(), withBucket(), withEnabledServices(), withServiceQuota(), withAnalyticsService(), or withEventingService() after container.start() (or after try-with-resources starts the container).","commonSituations":"Configuring the container inside a lifecycle hook that runs post-start, reusing a container instance across tests and reconfiguring it between tests, or setting a bucket inside an @Before callback that runs after container startup.","solutions":["Move all with*() configuration calls to before container.start().","Build configuration into a fresh CouchbaseContainer instance instead of mutating a running one.","If you need to run containers before the test body, use a static/singleton container pattern and configure it at declaration time.","Catch IllegalStateException as a programming-error signal and fix call ordering rather than handling it at runtime."],"exampleFix":"// before\nCouchbaseContainer container = new CouchbaseContainer();\ncontainer.start();\ncontainer.withBucket(new BucketDefinition(\"my-bucket\")); // throws\n// after\nCouchbaseContainer container = new CouchbaseContainer()\n    .withBucket(new BucketDefinition(\"my-bucket\"));\ncontainer.start();","handlingStrategy":"validation","validationCode":"if (container.isRunning()) {\n    throw new IllegalStateException(\"Configure the CouchbaseContainer before start()\");\n}\ncontainer.withBucket(bucket);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Configure all with*() options in the container declaration, never after start().","Use a builder/factory method that returns a fully configured container.","Never reconfigure shared/singleton containers between tests."],"tags":["lifecycle","illegal-state","configuration","couchbase"],"backgroundTag":"invalid-state-transition","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"}