flapdoodle-oss / de.flapdoodle.embed.mongo.spring

embedded mongo spring integration
Apache License 2.0
30 stars 7 forks source link

Is the latest 4.14.0 compatible with the spring boot 3.3.0? #54

Closed willgss closed 2 days ago

willgss commented 2 weeks ago

I am working on updating the spring boot to the latest 3.3.0 and found the "de.flapdoodle.embed.mongo" can not work correctly. We use it in our ut realized with spring boot test. we add "@AutoConfigureDataMongo" on the test entry class and use default setting for the mongodb(have no configuration in the "application.yml") and get the following errror:

2024-06-19 13:42:59.103+0800 INFO my-service [cluster-ClusterId{value='66726fe3c22c293ea8f49057', description='null'}-localhost:27017] [] [o.m.d.cluster.info] - Exception in monitor thread while connecting to server localhost:27017 com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.internal.connection.SocketStream.lambda$open$0(SocketStream.java:86) at java.base/java.util.Optional.orElseThrow(Optional.java:403) at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:86) at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:201) at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:153) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.net.ConnectException: Connection refused: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682) at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592) at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) at java.base/java.net.Socket.connect(Socket.java:751) at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:76) at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:105) at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:80) ... 4 common frames omitted

I did not find any useful solution for this error on the net, so please help me to check this issue. Thank you!

michaelmosmann commented 2 weeks ago

@willgss did it work before your update? I wonder why port 27017 is used, because that's the default port of mongodb and is not used by flapdoodle (use any free port aviable to avoid test collisions).

Hmm.. i will add spring 3.3.0 to the canary project (https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.canary) to see if anything changed.

willgss commented 2 weeks ago

@michaelmosmann Thank you for your quick response!

yes, it works correctly, my old spring boot is 2.6.6 and de.flapdoodle.embed.mongo is 3.4.5 , In my old code i did not add the annotation "@AutoConfigureDataMongo", when i meet this error after the update i add this annotation but it does not work, I do not know if it is necessary. About the port, in fact i used to assign the port 0, it works in old codes but raised a error of invalid port number after the update, i also try to assign it a new port such as 26016 but i got the same error i mentioned in this topic. I think de.flapdoodle.embed.mongo should has its default configuration to start the mongo database if i specified nothing, am i right? or i have to configure some necessary configuration? the following is my old configuration in application.yml. mongodb: embedded: version: 4.0.2 data: mongodb: host: 127.0.0.1 port: 0 it works in old codes but failed after update to 3.3.0

michaelmosmann commented 2 weeks ago

@willgss ah.. you upgrade from an old spring AND flapdoodle version. .. there were a lot of refactoring sind flapdoodle 3.x.x .. you should have a look at the canary project to se what you need to make it work..

This is a valid test: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.canary/blob/main/embed-spring-boot-31/src/test/java/example/ExampleIT.java

with some application properties: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.canary/blob/main/embed-spring-boot-31/src/main/resources/application.properties

If the canary projects works on your machine, then it should be easy to change it until it does not work anymore. Or the other way around.. remove stuff from your project until it works..

but i will try spring 3.3.x with that to see if its still working the same way..

michaelmosmann commented 2 weeks ago

btw: some more error messages around your failing test would be helpful.

willgss commented 2 weeks ago

@michaelmosmann your project is a little simple, here is part of my test class: @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(initializers = {ClientAndServerProvider.MockJsonConfigurationProvider.class}) @ActiveProfiles("test") @AutoConfigureDataMongo public class ComponentTestBase { }

I use the "de.flapdoodle.embed.mongo" in my component test, we need to start the service and do the api calling with the SpringBootTest framework, My service depends on the mongodb so we integrate the "de.flapdoodle.embed.mongo" in our test environment, the following is the error when i try to start the test, it seems the mongodb is not started successfully.: com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.internal.connection.SocketStream.lambda$open$0(SocketStream.java:86) at java.base/java.util.Optional.orElseThrow(Optional.java:403) at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:86) at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:201) at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:153) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.net.ConnectException: Connection refused: no further information at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682) at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592) at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) at java.base/java.net.Socket.connect(Socket.java:751) at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:76) at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:105) at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:80) ... 4 common frames omitted

I could not find the cause of this error, please help to check if the latest version works correctly with spring boot 3.3.0. I will try to run your example on my side. Thank you for your response!

willgss commented 2 weeks ago

@michaelmosmann I did not find the "application.properties" file under the "test" folder, so it is not necessary , am i right?

michaelmosmann commented 2 weeks ago

@willgss the property is in the main folder, not in test..

willgss commented 2 weeks ago

@michaelmosmann According to your example, i add the following dependency:

de.flapdoodle.embed de.flapdoodle.embed.mongo.spring3x 4.12.0 test

and specified the embeded db version: de.flapdoodle.mongodb.embedded.version=4.4.0.

It works now. In old version, we do not need these two items. I think they are mandatory after updating to spring boot 3.3.0/

michaelmosmann commented 2 days ago

@willgss spring 2.6.x did use a old flapdoodle-dependency.. with spring 2.7.x they removed the support (long story..) so you have to use this dependency as an replacement:)

michaelmosmann commented 2 days ago

@willgss so i close this issue.. reopen if needed.