I am trying to create a simple todo application using Quarkus, Kotlin, MongoDB and TestContainers. I am using testcontainers to test my integration with mongodb. Unfortunately my test is not working like I expected. Here is my test case, I am not sure what goes wrong. I can see from the logs that the test container for mongodb is started and I am setting the replicaSetUrl in to the property.
@QuarkusTest
class TodoResourceIT {
@Container
private val mongoDbContainer: MongoDBContainer = MongoDBContainer("mongo:4.2").withExposedPorts(27017)
init {
mongoDbContainer.start()
System.setProperty("quarkus.mongodb.connection-string", mongoDbContainer.replicaSetUrl)
}
@Test
fun shouldReturn200OK() {
given().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.body(Todo("1", "Find", "Find the letter F"))
.`when`().post("/api/todos").then().statusCode(200)
}
}
Here is my complete source code: https://github.com/faskan/todo-kotlin-quarkus
I have see a similar thread Integration testing with Testcontainers + Quarkus + MongoDB. But the solution works only in Java. Here is my sample implementation using Java which works perfectly fine. https://github.com/faskan/todo-java-quarkus
The problem I'm facing now is with Kotlin.
Stacktrace:
INFO [org.mon.dri.cluster] (cluster-ClusterId{value='60d34ff2ffb00164d8f60cbc', description='null'}-mongo:27017) Exception in monitor thread while connecting to server mongo:27017: com.mongodb.MongoSocketException: mongo
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211)
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75)
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.net.UnknownHostException: mongo
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:800)
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:886)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)
at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203)
Which is your quarkus version?.
You can QuarkusTestResourceLifecycleManager to archieve that, I've already tested it with kotlin and work fine. Here there is an example on how to do this