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

2

19

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]

Grunter answered 20/7, 2021 at 14:19 Comment(2)
Same for me, did you find the solution yet??Positively
nope, didnt find any solutionGrunter
B
10

I was facing the same issue and solved it by removing the annotation @Container from my container and starting it manually as a Singleton. The reason is that @Container was starting multiple containers. The first one was being successfully used by the tests and the other ones were throwing the com.mongodb.MongoSocketReadException: Prematurely reached end of stream exception.

Replacing this:


    @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);  
        }
    }

with this


    @Testcontainers  
    @DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)  
    class UserRepositoryTest {  
    
        static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:5.0.13");
    
        static {
            mongoDBContainer.start();
        }
    
        @DynamicPropertySource  
        static void setProperties(DynamicPropertyRegistry registry) {  
            registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);  
        }
    }

Butz answered 1/11, 2022 at 9:43 Comment(2)
Thanks! This workaround still works on SpringBoot 3.1Manufacturer
I lost 2 days suffering with it. This worked on SpringBoot 3.2.5Mirabelle
P
0

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);  
    }
}
Plumbiferous answered 3/9, 2022 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.