0

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)
Faisal Khan
  • 151
  • 1
  • 11

1 Answers1

0

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

Javier Toja
  • 1,630
  • 1
  • 14
  • 31
  • Thank you for the response. I was using a version lower than 2.0. I can upgrade my version to 2.0.0, but I couldn't find a documentation for the mongodb devservice. I was looking at https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc. Can you please help with the link for the mongodb devservice documentation? – Faisal Khan Jul 05 '21 at 21:40
  • yes @FaisalKhan there not a lot of documentation, the devServices are just for developing mode, i have to update the response, but i will provide you with an example in my repo of a integration test with kotlin and mongo which will work in any quarkus version – Javier Toja Jul 06 '21 at 08:58
  • @FaisalKhan check this repo https://github.com/javiertoja/stackoverflow/tree/main/kotlin-integration-tests-mongo, the important part is the class MockMongoDatabase, which implement the interface QuarkusTestResourceLifecycleManager. – Javier Toja Jul 06 '21 at 09:29
  • Thank you for the solution. It works perfectly! But I am a bit confused on why the behavior differs between Java and Kotlin implementation. I was expecting kotlin 'init' block to work like Java 'static' block. Is there something wrong with my understanding. – Faisal Khan Jul 07 '21 at 16:22