0
votes

I've been trying to create an embedded Mongodb database for the test, so far I wasn't able to make it work.

I've looked at https://github.com/quarkusio/quarkus/tree/main/test-framework/mongodb

I've also read this https://quarkus.io/guides/getting-started-testing#quarkus-test-resource but it doesn't seem updated


EDIT1:

application.properties (in my test package)

quarkus.mongodb.connection-string=${MONGO_URI:mongodb://localhost:19345/db-test}`

When I run my test I get the following error: Cluster description not yet available. Waiting for 30000 ms before timing out and then Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:19345, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]


EDIT2:

application.properties

%prod.quarkus.mongodb.connection-string=...
%prod.quarkus.mongodb.database=prod-db

test

@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class ControllerTest {
    @Inject lateinit var accountRepository: MockAccountRepository

    @Test
    @Order(1)
    fun `badRequest`() {
        given()
            .`when`().get(endPoint)
            .then()
            .assertThat().statusCode(HttpResponseStatus.BAD_REQUEST.code())
    }
}

MockAccount

@Mock
@ApplicationScoped
class MockAccountRepository(mongoClient: MongoClient) {

    private val collection = mongoClient.getDatabase("db-test")
        .getCollection("account", MockAccount::class.java)

    ....
}
1
What version of quarkus are you using? - Javier Toja
I'm using quarkus 2.1.2.Final - Biscuit

1 Answers

0
votes

You have two options, Quarkus 2.0+ version come with devServices support, which will start automatically a mongo database or other resources required to run your container, which are currently supported for the dev services module. Here you can see information about this feature https://quarkus.io/guides/mongodb#dev-services This mongo database will available also for unit testing.

While using the devServices its important to do not override or define the configuration property quarkus.mongodb.connection-string for the dev or test profile if order to let quarkus handle the connection by itself.


If you want to start a different mongodb or just not use this feature you can create a new QuarkusTestResource where you can start whatever required dependency and manage its lifecycle. Here you have an example in kotlin https://github.com/javiertoja/stackoverflow/tree/main/kotlin-integration-tests-mongo .

As summary you have to create a class that implements the interface QuarkusTestResourceLifecycleManager like for example:

class MockMongoDatabase : QuarkusTestResourceLifecycleManager {

    private val mongoDBContainer = MongoDBContainer(DockerImageName.parse("mongo:4.0.10"))

    override fun start(): MutableMap<String, String> {
        println("STARTING MONGO ")
        mongoDBContainer.start()

        return mutableMapOf(Pair("quarkus.mongodb.connection-string",mongoDBContainer.replicaSetUrl))
    }

    override fun stop() {
        println("STOPPING MONGO")
        mongoDBContainer.stop()
    }
}

After that you will have to annotate the Test classes that require this resource to operate with the annotation @QuarkusTestResource(MockMongoDatabase::class). This will instruct quarkus to startup this resource before the tests and shutdown it after finalizing.


If you want you can initialize your test resource with data in your test cases using the normal MongodbClient or whithin the QuarkusTestResourceLifecycle class which implements the resource.

Beware of mixing both because by default quarkus will try to use the devservices support if you don't disable it or you don't configure the connection properties.