Mongo driver with @Testcontainers, throws exceptions after tests run successfully

Viewed 668

I'm setting up integration tests in a sample spring boot kotlin project, using testcontainers:mongodb.

I've set up a MongoDBContaine, and everything works as expected - the app connects to the mongodb and tests with repositories (e.g. save, delete) work perfectly fine, but I've noticed that after the tests ran (successfully) mongodb.driver throws an exception in the end - would seem like the container isn't being gracefully closed/stopped - is that possible?

Example of how I start the container

companion object {
        @Container
        var mongoDBContainer = MongoDBContainer("mongo:4.4.2")

        @JvmStatic
        @DynamicPropertySource
        fun setProperties(registry: DynamicPropertyRegistry) {
            registry.add("spring.data.mongodb.uri") { mongoDBContainer.replicaSetUrl }
        }
    }

Test method

@Test fun someTest() {
   autowiredRepository.save(document)
   ...
   ...
}

As for the running class, I'm simply using the @Testcontainers annotation on top of it with @SpringBootTest, nothing else.

The exception I'm getting in the end is

com.mongodb.MongoSocketReadException: Prematurely reached end of stream
    at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112) ~[mongodb-driver-core-4.2.3.jar:na]
    at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:131) ~[mongodb-driver-core-4.2.3.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:647) ~[mongodb-driver-core-4.2.3.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveMessageWithAdditionalTimeout(InternalStreamConnection.java:512) ~[mongodb-driver-core-4.2.3.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:355) ~[mongodb-driver-core-4.2.3.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receive(InternalStreamConnection.java:315) ~[mongodb-driver-core-4.2.3.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:215) ~[mongodb-driver-core-4.2.3.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.2.3.jar:na]
    at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]

1 Answers

Did you try to exclude the default mongoEmbedded which is provided by spring?

I don't know in Kotlin but in Java it's something like that:

@Testcontainers  
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)  
class UserRepositoryTest {  

    @Container  
    static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:5.0.10");  

    @DynamicPropertySource  
    static void setProperties(DynamicPropertyRegistry registry) {  
        registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);  
    }
}
Related