Doing implementation of a microservice with a few endpoints based on Spring Boot
and MongoDB
and trying to write integration tests using @SpringBootTest
annotation capabilities.
At the moment, I am facing an issue that I need to pre-populate an embedded MongoDB
instance that instantiated only during 'test' phrase with some test data.
And I did not find any out-of-the-box option available in Spring Boot
for this purpose.
Some people advice to use for test data pre-populating tools like mongobee or mongoprefill or nosql-unit but for me, it seems like overhead or workaround, do not want to introduce any new dependencies even in test scope.
So could you please advice: In the current Spring Boot
ecosystem, what is the right way to pre-populate MongoDB
for testing purpose, when we are talking about integration (end-to-end) testing with @SpringBootTest
?
There are multiple ways to pre-populate data:
@BeforeEach
,@BeforeAll
to fill in data@Sql
feature we have for testing relational databases and write something similar using theAsbtractTestExectuionListener
. For this have a look at the Spring classSqlScriptsTestExecutionListener
CommandLineRunner
interface and only activate this bean for your integration test profile with@Profile("integration-test")