jackjackjack
jackjackjack

Reputation: 1

Embedded MongoDB with Spring not working in test

I am trying to create a test class with embedded MongoDB. I can't start my test because it seems that embedded mongodb has not started.

The exception that I get is the following:

2021-10-18 17:33:17 INFO  - [org.mongodb.driver.cluster:76] - Exception in monitor thread while connecting to server localhost:27019
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:272)
    at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:129)
    at java.base/sun.nio.ch.Invoker.invokeDirect(Invoker.java:158)
    at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:186)
    at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:298)
    at java.base/sun.nio.ch.WindowsAsynchronousSocketChannelImpl$ConnectTask.failed(WindowsAsynchronousSocketChannelImpl.java:308)
    at java.base/sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:389)
    at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.io.IOException: The remote computer refused the network connection.

The dependency for embedded mongo:

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

This is my test class

import ...

@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@EnableReactiveMongoRepositories
@ComponentScan(
    excludeFilters = {@ComponentScan.Filter(
            type = FilterType.ASSIGNABLE_TYPE,
            value = {MongoReactiveDataAutoConfiguration.class}
    )}
)
@Slf4j
public class MyServiceTest {

    @Autowired
    private MyService myService;


    @Test
    public void saveDataOnMongoTest(){
      
    }
}

This is my configuration of mongo on my yaml under the test profile

  data:
    mongodb:
      uri: mongodb://localhost:27019/myDb
      database: myDb
      port: 27019  # embedded test purpose
      repositories:
        type: reactive
    jpa:
      repositories:
        enabled: true

  mongodb:
    embedded:
      version: 4.0.3
      features: sync_delay,no_http_interface_arg,only_with_ssl,only_windows_2008_server

Upvotes: 0

Views: 3512

Answers (2)

mikeb
mikeb

Reputation: 11267

Found this because I had the same issue.

I fixed it by adding the following to the src/test/resources/application.properties:

spring.data.mongodb.port=0
spring.mongodb.embedded.version=3.6.5

Port -> 0 means use a random port and the version must be set so it will download and start a mongo db.

This is Kotlin code but it's the test that works for me:

@DataMongoTest
@ExtendWith(SpringExtension::class)
class HolmesDataApplicationTests {

    @Test
    fun contextLoads() {
    }

    @Test
    fun test(@Autowired mongoTemplate: ReactiveMongoTemplate) {
        val objectToSave = BasicDBObjectBuilder.start()
            .add("key", "value")
            .get()
        mongoTemplate.save(objectToSave, "collection")
    }
}

Upvotes: 1

Neeraj
Neeraj

Reputation: 131

Add exclusion annotation to the main class like

@SpringBootTest @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})

It may be working.

Upvotes: 0

Related Questions