我正在使用 testcontainers:mongodb 在示例 spring boot kotlin 项目中设置集成测试。
我已经设置了一个 MongoDBContaine,一切都按预期工作 - 应用程序连接到 mongodb 并使用存储库进行测试(例如保存、删除)工作得非常好,但我注意到在测试运行(成功)之后 mongodb.driver最后抛出异常 - 看起来容器没有被优雅地关闭/停止 - 这可能吗?
我如何启动容器的示例
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 fun someTest() {
autowiredRepository.save(document)
...
...
}
至于运行类,我只是在它上面加上@SpringBootTest 的@Testcontainers 注解,仅此而已。
我最后得到的例外是
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]