I'm trying to create a Integration test of a Spring Boot controller running against a MongoDB embedded database with Spring 3.x but I have been unable to configure it correctly. As I have read if I'm using @SpringBootTest and @AutoConfigureDataMongo it will try to connect with a real database instead of searching for the embbeded one.
I have seen this responses:
in stackoverflow to manually start the embedded database.
But I cannot use this code with de.flapdoodle.embed.mongo version 4.9.2 since the code has changes and those classes are not longer available.
Anyone can help me to manually start the database in this case?
The solution above should work. I used it and it worked.
first, make sure you have a profile for your test env eg. application-test.properties preferably in your src/test/resources directory
mine has this configuration in it
#Test MongoDB config
spring.data.mongodb.uri=mongodb://localhost/test
next, add the embedded Mongo DB dependency
<!-- https://mvnrepository.com/artifact/de.flapdoodle.embed/de.flapdoodle.embed.mongo -->
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
On your IT(Integration Test class), add these annotations
@SpringBootTest
@AutoConfigureMockMvc
@AutoConfigureDataMongo
@ActiveProfiles("test")
class UserControllerIT {
//Your code here
}
Now run your test and it should work, if you want to use a real db in your test just change the db URL and it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With