{"record":{"id":"f63553fe35ac8a33","repo":"testcontainers/testcontainers-java","slug":"mongodbcontainer-should-be-started-first-f63553","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/MongoDBAtlasLocalContainer.java","lineNumber":64,"sourceCode":"\n    /**\n     * Gets a database specific connection string for the default {@value #MONGODB_DATABASE_NAME_DEFAULT} database.\n     *\n     * @return a database specific connection string.\n     */\n    public String getDatabaseConnectionString() {\n        return getDatabaseConnectionString(MONGODB_DATABASE_NAME_DEFAULT);\n    }\n\n    /**\n     * Gets a database specific connection string for a provided <code>databaseName</code>.\n     *\n     * @param databaseName a database name.\n     * @return a database specific connection string.\n     */\n    public String getDatabaseConnectionString(final String databaseName) {\n        if (!isRunning()) {\n            throw new IllegalStateException(\"MongoDBContainer should be started first\");\n        }\n        return baseConnectionString() + \"/\" + databaseName + \"?\" + DIRECT_CONNECTION;\n    }\n}\n","sourceCodeStart":46,"sourceCodeEnd":69,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/mongodb/src/main/java/org/testcontainers/mongodb/MongoDBAtlasLocalContainer.java#L46-L69","documentation":"MongoDBAtlasLocalContainer.getDatabaseConnectionString(databaseName) appends the database name to the base connection string of the Atlas Local container. If the container is not running, it throws this IllegalStateException since there is no base connection string yet.","triggerScenarios":"Calling getDatabaseConnectionString(...) before start(), after stop(), or in a context where the container start failed (e.g. Docker not available), or wiring the connection string before the Testcontainers extension starts the container.","commonSituations":"Using the Atlas Local container like a plain config object and reading the URL during test configuration loading; @BeforeAll ordering where the accessor runs before @Container startup.","solutions":["Call start() (or use @Container/@Testcontainers) before accessing the connection string","Verify Docker is running and the container actually started","Defer MongoClient creation to a @BeforeAll that runs after container startup"],"exampleFix":"// before\nMongoDBAtlasLocalContainer atlas = new MongoDBAtlasLocalContainer(...);\nString url = atlas.getDatabaseConnectionString(\"app\"); // throws\n// after\nMongoDBAtlasLocalContainer atlas = new MongoDBAtlasLocalContainer(...);\natlas.start();\nString url = atlas.getDatabaseConnectionString(\"app\");","handlingStrategy":"validation","validationCode":"if (!atlas.isRunning()) {\n    atlas.start();\n}\nString url = atlas.getDatabaseConnectionString(\"app\");","typeGuard":null,"tryCatchPattern":"try {\n    String url = atlas.getDatabaseConnectionString(\"app\");\n} catch (IllegalStateException e) {\n    throw new IllegalStateException(\"Start MongoDBAtlasLocalContainer before reading the connection string\", e);\n}","preventionTips":["Wire the container with @Container so startup is ordered before use","Check Docker availability in the environment first","Defer connection-string consumers until after startup"],"tags":["mongodb","atlas","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"}