Unit testing - how to avoid mongodb-panache client connection to mongodb instance

1,313 views
Skip to first unread message

Gianfelice Catini

unread,
Feb 27, 2021, 7:16:29 AM2/27/21
to Quarkus Development mailing list
Hi all,
I'm unit testing my Quarkus application that is a service layer that integrates mongoDb for CRUD operations and text-based search operations.
What I'm trying to achieve is a real unit testing so I have mocked my service layer and my panache repository (I have used the repository pattern) classes.
The unit tests I wrote work fine, but when I launch them I see in the console log the mongodb client that want to connect to a mongodDB instance (I mean during the Quarkus application bootstrap).
I tryed to remove from my test configuration profile the following two properties:
  • quarkus.mongodb.connection-string
  • quarkus.mongodb.database
 but Quarkus complains about that because it expects that properties have values.
There's a way to tell Quarkus "dont try to connect to mongodb instance at bootstrap" for a specific configuration profile?

Thank you.

tkhm

unread,
Feb 27, 2021, 11:04:08 PM2/27/21
to Quarkus Development mailing list
I had the same issue before.

I added @Default annotation on my in-memory database repository(for unit testing) and also added @IfBuildProfile("prod") on my actual database connection repository.
So, when you run your unit test, in-memory repo will be loaded because unit test will use "test" profile. In other words, actual db connection(in this case, mongodb) will not be required.

Gianfelice Catini

unread,
Feb 28, 2021, 6:37:34 AM2/28/21
to Quarkus Development mailing list
Thank you for your answer. Can you please post the most significant snippets from your code? Just few lines to understand how you did it.
Did you follow a specific documentation?

I'm not using a in-memory mongoDB right now, but I'll do because I would like to test my text-based search logic against a mock db.
Thank you.

tkhm

unread,
Mar 1, 2021, 1:53:12 AM3/1/21
to Quarkus Development mailing list
My original working code is not sharable, so I made a small sample for you. I've pushed the codes on GitHub, so could you check it once, please?

I think you'll be interested in the following files:

You're using Mongodb with Panache and I'm using Hibernate with Panache, so it is not the same but it might be some help.

note: when I tried this first time, I'm just only using GreetingModelDto.java class, but I found if I call this class in my unit test, it try to find data source and try to make a connection.(in other words, unit test will be failed if it can't make a connection to DB)
To avoid this, I made DTO for Hibernate/Panache and transform it to pojo.

ref: 
- Quarkus - Configuring Your Application https://quarkus.io/guides/config#configuration-profiles

Gianfelice Catini

unread,
Mar 4, 2021, 6:23:51 PM3/4/21
to Quarkus Development mailing list
tkhm,
thank you for your example and I'm sorry for my late answer.
I cloned your repo few minutes ago and looking at your code I catched what you said in your previous post.
I really appreciate your help.
Message has been deleted

Hugo Lavalle

unread,
Oct 19, 2021, 4:14:36 PM10/19/21
to Quarkus Development mailing list
Hi did you see this: https://quarkus.io/guides/mongodb#testing-helpers ?

I had the same issue. I solved with:
- Add io.quarkus:quarkus-test-mongodb dependency to your pom.xml
- Add annotation @QuarkusTestResource(MongoTestResource.class) at the test class, just below @QuarkusTest

Be aware you don't have already a mongodb running on localhost:27017, flapdoodle uses the same port. MongoTestResource has 27017 hard coded. You can implement for example a MyMongoTestResource and change the port number, it worked for me.
Reply all
Reply to author
Forward
0 new messages