I have a TestContainers test with MongoDB, where it seems problematic to set the default username / password credentials. This test below works without credentials:
@Testcontainers
@SpringBootTest
public class SpringContextWithMongoTest
{
@Container
static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.20-jammy")
.withExposedPorts(27017)
.waitingFor( Wait.forListeningPort() );
@DynamicPropertySource
static void registerMongoProperties( DynamicPropertyRegistry registry ) {
mongoDBContainer.start();
registry.add("spring.data.mongodb.host", mongoDBContainer::getHost);
registry.add("spring.data.mongodb.port", mongoDBContainer::getFirstMappedPort);
}
@Test
public void contextLoads() {
}
}
However, the main app is connecting to a MongoDB which does have credentials, and these are configured under src/main/resources/application.properties
so:
spring.data.mongodb.username=mongo
spring.data.mongodb.password=mongo
spring.data.mongodb.database=stackoverflow
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
These properties credentials interfere with the test running.
I've tried created a file src/test/resources/application-test.properties
with blank values:
spring.data.mongodb.username=
spring.data.mongodb.password=
I've also tried changing the dynamic properties method:
@DynamicPropertySource
static void registerMongoProperties( DynamicPropertyRegistry registry ) {
mongoDBContainer.start();
registry.add("spring.data.mongodb.host", mongoDBContainer::getHost);
registry.add("spring.data.mongodb.port", mongoDBContainer::getFirstMappedPort);
registry.add("spring.data.mongodb.username", () -> null);
registry.add("spring.data.mongodb.password", () -> null);
}
This also fails to get the test working. I believe the test fails because we are supplying credentials when Mongo expected none.
The only I have found that works, is to comment out the values in src/main/resources/application.properties
, but this is not useful for the main boot up of the app.
#spring.data.mongodb.username=mongo
#spring.data.mongodb.password=mongo
I know this may seem like an X-Y problem, in that, maybe I should be attempting to configure the Test Container Mongo to require credentials, but I still think this is a reasonable question to ask:
How can you unset Spring Boot properties for a test?
It seems the answer is:
No, it's not possible.
Which means you need to use properties which don't need to be "unset".
So instead of
spring.data.mongodb.username=mongo
spring.data.mongodb.password=mongo
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
Use the single property of
spring.data.mongodb.uri=mongodb://mongo:mongo@localhost:27017
Which includes the host, port, username and password all in a single property, but which doesn't need to contain the latter two.
So in a test scenario, you can do this, and it will work:
@DynamicPropertySource
static void registerMongoProperties( DynamicPropertyRegistry registry, MongoDBContainer mongoDBContainer) {
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl );
}