{"record":{"id":"f9bc96759031b8b3","repo":"testcontainers/testcontainers-java","slug":"mongodbcontainer-should-be-started-first-f9bc96","errorCode":null,"errorMessage":"MongoDBContainer should be started first","messagePattern":"MongoDBContainer should be started first","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/mongodb/src/main/java/org/testcontainers/mongodb/MongoDBContainer.java","lineNumber":203,"sourceCode":"\n    /**\n     * Gets a replica set url for the default {@value #MONGODB_DATABASE_NAME_DEFAULT} database.\n     *\n     * @return a replica set url.\n     */\n    public String getReplicaSetUrl() {\n        return getReplicaSetUrl(MONGODB_DATABASE_NAME_DEFAULT);\n    }\n\n    /**\n     * Gets a replica set url for a provided <code>databaseName</code>.\n     *\n     * @param databaseName a database name.\n     * @return a replica set url.\n     */\n    public String getReplicaSetUrl(String databaseName) {\n        if (!isRunning()) {\n            throw new IllegalStateException(\"MongoDBContainer should be started first\");\n        }\n        return getConnectionString() + \"/\" + databaseName;\n    }\n}\n","sourceCodeStart":185,"sourceCodeEnd":208,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/mongodb/src/main/java/org/testcontainers/mongodb/MongoDBContainer.java#L185-L208","documentation":"getReplicaSetUrl(databaseName) in org.testcontainers.mongodb returns connection string + '/' + databaseName, but only if the container is running. Otherwise it throws this IllegalStateException, since no connection string exists before startup.","triggerScenarios":"Invoking getReplicaSetUrl before container.start(), after stop(), or when start() failed (e.g. Docker daemon unavailable), or when the wrong (unstarted) container instance is referenced.","commonSituations":"Static client initialization before Testcontainers lifecycle; referencing a container after the test class torn it down; assuming a shared singleton container is already running.","solutions":["Ensure start() (or @Container) has run before calling getReplicaSetUrl","Use the static + @Testcontainers/@Container singleton pattern so the same instance is started","Check that container startup actually succeeded (no swallowed exceptions) before use"],"exampleFix":"// before\nMongoDBContainer mongo = new MongoDBContainer(\"mongo:6\");\nString url = mongo.getReplicaSetUrl(\"orders\"); // not started\n// after\n@Testcontainers\nstatic MongoDBContainer mongo = new MongoDBContainer(\"mongo:6\");\n@Container\n// start handled by extension; then in test:\nString url = mongo.getReplicaSetUrl(\"orders\");","handlingStrategy":"validation","validationCode":"if (!mongo.isRunning()) {\n    throw new IllegalStateException(\"Call mongo.start() before getReplicaSetUrl()\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    String url = mongo.getReplicaSetUrl(\"orders\");\n} catch (IllegalStateException e) {\n    throw new IllegalStateException(\"MongoDBContainer lifecycle error — ensure @Container startup ran\", e);\n}","preventionTips":["Use the static @Container pattern with @Testcontainers","Do not call container getters in static initializers or constructors","Verify Docker is running so start() cannot silently fail"],"tags":["mongodb","testcontainers","lifecycle","illegal-state"],"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"}