flapdoodle-oss / de.flapdoodle.embed.mongo

...will provide a platform neutral way for running mongodb in unittests.
Apache License 2.0
907 stars 161 forks source link

@DataMongoTest in Spring Boot 3.0.0 #439

Closed fkreis closed 1 year ago

fkreis commented 1 year ago

I was following this great and simple article to test my custom Repository implementations: https://www.baeldung.com/spring-boot-embedded-mongodb

I was using Spring Boot 2.7.4 so far in conjunction with de.flapdoodle.embed.mongo 3.4.11 (that's the version from Spring Boots' dependency management). With that setup the tests worked great (thank you for the great tool)!

However, after updating to Spring Boot 3.0.0 the tests don't work anymore. I tried it with several versions of de.flapdoodle.embed.mongo (3.4.11, 4.2.0, 4.3.0). It always fails with

MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]

I don't see ANY logs that refer to even the attempt to start a mongo instance. Looks to me as if the autoconfiguration does not work anymore. Is there any guide on how to do the test setup with Spring Boot 3.0.0?

I tried to follow the readme.mds trough the project https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/tree/spring-3.0.x https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/spring-3.0.x/HowTo.md

However, I can't make it work based on that documentation, especially as it is not clear to me whether I need de.flapdoodle.embed.mongo AND de.flapdoodle.embed.mongo.spring30x or only the second one and in which versions? It is particularly confusing as it says in de.flapdoodle.embed.mongo.spring30x's readme, that the project is based on Spring 2.7.x

Can you point to simple documentation that explains how to do the setup for Spring Boot 3? :)

michaelmosmann commented 1 year ago

@fkreis spring 3.0.0 removed its own flapdoodle-embed-mongo integration.. but the implementation of my spring integration is based on the 2.7.x version.. (yeah, maybe not very helpful to mention this.. hmm:))

you need de.flapdoodle.embed.mongo AND de.flapdoodle.embed.mongo.spring30x .. and there you should use the latest versions - 4.3.1 and 4.3.2

michaelmosmann commented 1 year ago

@fkreis i will try to update project: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.canary with a working spring 3.x sample

fkreis commented 1 year ago

@michaelmosmann thanks a lot for your quick response! Adding both dependencies with the mentioned versions to my project made it (almost) work again. Additionally I hat to change

spring.mongodb.embedded.version to de.flapdoodle.mongodb.embedded.version but this was pretty easy to guess from the logs.

I think it is a really good idea to create a new 3.x example, would have helped me a lot! :)

Another idea might be to write a little new section in your readme, like


Migration from Spring Boot 2.7.x to 3.0.x

Update your de.flapdoodle.embed.mongo dependency to the latest version, e.g.

             <dependency>
                <groupId>de.flapdoodle.embed</groupId>
                <artifactId>de.flapdoodle.embed.mongo</artifactId>
                <version>4.3.1</version>
            </dependency>

Add a new dependency to your project, as Spring has removed its own flapdoodle-embed-mongo integration:

       <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo.spring30x</artifactId>
            <version>4.3.2</version>
        </dependency>

In your test properties, replace spring.mongodb.embedded.version with de.flapdoodle.mongodb.embedded.version, e.g.:

@DataMongoTest(
        properties = {
                "spring.data.mongodb.database=test",
                "spring.data.mongodb.port=27017",
                "de.flapdoodle.mongodb.embedded.version=5.0.6",
        }
)
michaelmosmann commented 1 year ago

@fkreis ah. thanks.. i think i will add this to the Readme:)

michaelmosmann commented 1 year ago

@fkreis i will close this issue.. thank you:)