-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow users to specify a mongo
database name
#2876
Comments
Why do you need it? You can just access/create a custom instance using |
When one uses Testcontainer’s reuse feature, a framework and a database per a test as a way to separate(isolate) data, then specifying a database might be helpful. Given that I’ll submit a PR to add a withDatabaseName method for the MongoDBContainer. If the withDatabaseName mothod is not called, then a database name will default to "test" as now. |
This reverts commit ccc0f40
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this. |
Updated a corresponding PR |
@silaev How Can I solve this problem? In order to isolates each test from my test-class (my goal is to use PArallel tests, but for that I need isolates each test). Consequently, I need a different database-name in each test. For that, I need to change the TestContainer-Databasename. I am trying to use getReplicaSetUrl(String databaseName), but it is not working. How can I solve this problem? |
@PauloPortfolio Do you use a framework? Separation per the method of the class might not be so easy depending on a framework. The key idea is to provide a database name to a MongoDB client and enable the Testcontainers' reuse feature. Here is an example for the Testcontainers' MongoDB Module and Spring Data MongoDB to separate test classes (not methods!) per database names:
@DataMongoTest
@ContextConfiguration(initializers = IT1Test.Initializer.class)
class IT1Test {
private static final MongoDBContainer MONGO_DB_CONTAINER =
new MongoDBContainer(DockerImageName.parse("mongo:4.4.2"))
.withReuse(true);
@Autowired
private ProductDao productDao;
@BeforeAll
static void setUpAll() {
MONGO_DB_CONTAINER.start();
}
@AfterAll
static void tearDownAll() {
if (!MONGO_DB_CONTAINER.isShouldBeReused()) {
MONGO_DB_CONTAINER.stop();
}
}
@Test
void test1() {
Product product1 = ProductTestUtil.generateProduct(120589L, "AAA", Brand.DOLCE, BigDecimal.valueOf(9), 9, Size.SIZE_50);
Product product2 = ProductTestUtil.generateProduct(120590L, "BBB", Brand.DOLCE, BigDecimal.valueOf(15.69), 6, Size.SIZE_100);
Product product3 = ProductTestUtil.generateProduct(120591L, "CCC", Brand.ENGLISH_LAUNDRY, BigDecimal.valueOf(55.12), 3, Size.SIZE_100);
StepVerifier.create(productDao.insert(Flux.just(product1, product2, product3)))
.expectNextCount(3L)
.verifyComplete();
}
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
String.format(
"spring.data.mongodb.uri: %s",
MONGO_DB_CONTAINER.getReplicaSetUrl("my-db" + UUID.randomUUID().getMostSignificantBits())
)
).applyTo(configurableApplicationContext);
}
}
}
[
"admin",
"config",
"local",
"my-db-5289724188915119626",
"my-db-6086375598922775794",
"my-db-79157823489553256",
"my-db5367856646571576153"
]
|
Closing this issue due to it was implemented via #2980 |
testcontainers-java/modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java
Line 22 in 5566597
The text was updated successfully, but these errors were encountered: