Spring-boot: embedded mongodb - ConnectException: Connection refused: connect

Created on 20 Mar 2018  路  6Comments  路  Source: spring-projects/spring-boot

In this example spring boot project I am trying to test the MongoRepository and the Service class that uses it in a SpringBootTest.

However, spring boot doesn't seem to use an embedded mongodb but fails to connect (Connection refused) because it cannot find a real mongo process running on the test port.

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_92]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_92]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_92]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_92]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_92]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_92]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_92]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_92]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongodb-driver-core-3.6.3.jar:na]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.6.3.jar:na]
    ... 3 common frames omitted

This is the test

@RunWith(SpringRunner.class)
@SpringBootTest(classes=MongoConfig.class)
@TestPropertySource(locations = "classpath:application-test.properties")
public class Test_TimezoneDao {

    private static final Logger LOG = LoggerFactory.getLogger(Test_TimezoneDao.class);

    @Autowired private TimezoneDao dao;
    @Autowired private CounterService counterService;


    @Test
    public void test() 
    { /*...*/ }
}

using this config class

@Profile("test")
@SpringBootConfiguration
@ComponentScan(basePackages= {"com.example.demomongodb"})
@EnableMongoRepositories(basePackages= {"com.example.demomongodb"})
@EnableAutoConfiguration
@AutoConfigureDataMongo
public class MongoConfig 
{ }

In the pom I have these dependencies

              <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

                <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>

In the application-test.properties I tried both a mongodb port of 0 (random) and non-zero (deterministic) as described in the docs

spring.data.mongodb.port=30000
spring.data.mongodb.database=mongo-example

I did try to use the embedmongo-spring dependency from cz.jirutka.spring and create Mongo bean in the config but it doesn't help.

Thanks for the help!

Most helpful comment

Looks like someone (either flapdoodle or spring) cannot handle uri-based approach of creating and connecting to embedded mogo. Might be bindIp-related issues or I am doing something wrong :)
Worked with spring-boot 1.4.x, doesn't with spring-boot 1.5.x and 2.0.x.

If I configure my mongo with
spring.data.mongodb.{host, port, database} (all three set explicitly) - OK

spring.data.mongodb.uri - problem

All 6 comments

@MaxHoefl I'm unable to reproduce the issue with the sample you've provided. On running Test_TimezoneDao, I can see that the connection to the embedded mongo is open

o.s.b.a.mongo.embedded.EmbeddedMongo     :  I CONTROL  [initandlisten] MongoDB starting

@MaxHoefl try removing the scope test from your pom.xml on the embedded mongo dependency. With the scope in there the embedded will only work on your tests and wont start on a regular run of the program.

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

Looks like someone (either flapdoodle or spring) cannot handle uri-based approach of creating and connecting to embedded mogo. Might be bindIp-related issues or I am doing something wrong :)
Worked with spring-boot 1.4.x, doesn't with spring-boot 1.5.x and 2.0.x.

If I configure my mongo with
spring.data.mongodb.{host, port, database} (all three set explicitly) - OK

spring.data.mongodb.uri - problem

Looks like someone (either flapdoodle or spring) cannot handle uri-based approach of creating and connecting to embedded mogo. Might be bindIp-related issues or I am doing something wrong :)
Worked with spring-boot 1.4.x, doesn't with spring-boot 1.5.x and 2.0.x.

If I configure my mongo with
spring.data.mongodb.{host, port, database} (all three set explicitly) - OK

spring.data.mongodb.uri - problem

thanks for your sharing!

Was this page helpful?
0 / 5 - 0 ratings