DEV Community

Discussion on: The Testcontainers’ MongoDB Module and Spring Data MongoDB in Action

Collapse
 
silaev profile image
silaev

Yes, it does. If need be, you can separate data via a database name.
If you don’t use a framework, you are free to go with MongoClient#getDatabase("my-db").
However, a framework (for instance, Spring Data MongoDB) might get a db name from a url, that one can get via MongoDBContainer#getReplicaSetUrl(). There are 2 choices:
1) add as a workaround your db name to a url, like MongoDBContainer#getReplicaSetUrl() + "-my-db", which, for example, results in mongodb://localhost:32880/test-my-db. Just make sure that such a db name is unique for each test;
2) wait for this PR to be merged.

Collapse
 
alevohin profile image
Yuriy

Tried your workaround solution on your PatchProductLoadITTest and ProductControllerITTest by adding:

  • testcontainers.reuse.enable=true to .testcontainers.properties
  • .withReuse(true) to MONGO_DB_CONTAINER
  • MONGO_DB_CONTAINER.getReplicaSetUrl()+"-my-db-N" to both tests (N=1 for PatchProductLoadITTest and N=2 for ProductControllerITTest) But a container is not reused. How can I make it work?
Thread Thread
 
silaev profile image
silaev

Put it simply, my tests stop each MongoDBContainer in @AfterAll methods. I’ve added if (!MONGO_DB_CONTAINER.isShouldBeReused()) statement to stop MongoDBContainer only if we do not use Testcontainers' reusable feature. See this commit for more details. Thanks for you comment.