Using spring-boot-autoconfigure 2.6.3

In my application.properties i have this uri as a convenient default:

spring.data.mongodb.uri = mongodb://127.0.0.1:27017/${spring.application.name}

My test class looks like this:

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {Application.class, EmbeddedMongoAutoConfiguration.class},
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        properties = {"spring.data.mongodb.port=0", "spring.mongodb.embedded.version=3.6.5"})
@ActiveProfiles({"testing"})
public class ApiTest {
    ...
}

When starting up, i get the exception:

Caused by: java.lang.IllegalStateException: Invalid mongo configuration, either uri or host/port/credentials/replicaSet must be specified
    at org.springframework.util.Assert.state(Assert.java:76)
    at org.springframework.boot.autoconfigure.mongo.MongoPropertiesClientSettingsBuilderCustomizer.validateConfiguration(MongoPropertiesClientSettingsBuilderCustomizer.java:61)
    at org.springframework.boot.autoconfigure.mongo.MongoPropertiesClientSettingsBuilderCustomizer.customize(MongoPropertiesClientSettingsBuilderCustomizer.java:52)
    at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.customize(MongoClientFactorySupport.java:55)
    at org.springframework.boot.autoconfigure.mongo.MongoClientFactorySupport.createMongoClient(MongoClientFactorySupport.java:49)
    at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration.mongo(MongoAutoConfiguration.java:55)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 192 more

When i remove the default uri from default application.properties, then the test just works fine.

However, my expectation is, that when an embedded mongo database is used, then the client should be setup to connect to the embedded database, ignoring the uri.

Technically, when looking at the code, the MongoPropertiesClientSettingsBuilderCustomizer just needs to ignore the uri, when it calls validateConfiguration(), when the embedded port is set.

It does already so, when it later calls applyHostAndPort(), where it overrides the uri with a new one, so that the client connects to the embedded database.

I think this is a regression from #8219 that originated from #6739

Thank you.

Comment From: wilkinsona

Embedded Mongo will allocate an ephemeral port by default so there's no need to set spring.data.mongodb.port to 0. As far as I can tell, you will get the behaviour that you want if you remove this property from your test. Can you please give that a try and let us know how it goes?

Comment From: okrische

Hello,

i can confirm. If i remove "spring.data.mongodb.port=0" from the properties in the @SpringBootTest, I get an embedded mongo at random port as well and the uri is ignored.

I also see it mentioned here, that the default is a random port.

  • https://docs.spring.io/spring-boot/docs/2.6.3/reference/html/data.html#data.nosql.mongodb.embedded

The port that Mongo listens on can be configured by setting the spring.data.mongodb.port property. To use a randomly allocated free port, use a value of 0. The MongoClient created by MongoAutoConfiguration is automatically configured to use the randomly allocated port.

And later as a note:

If you do not configure a custom port, the embedded support uses a random port (rather than 27017) by default.

I still think, setting value to 0 or not setting it at all, both cases lead to a random port and therefore behaviours could be equivalent and the uri can be ignored. But iam happy that it works. :-)

Thank you for the quick response. It never came as an idea to remove the setting.