I have a Spring Boot application with some REST endpoints and I have a MongoDB. I would like to create a test that runs every time the project is built. The test should simply take some mock data and put it into the database and use that mock data to test that the endpoints return the correct results. If it's possible, I don't want the mock data to actually go all the way to the database. I believe I've seen something before where Spring could simulate the database. Can anyone point me in the right direction?
CodePudding user response:
Here is an example for testing JPA Queries with Spring Boot and @DataJpaTest
https://reflectoring.io/spring-boot-data-jpa-test/
CodePudding user response:
You can specify a different datasource configuration for testing. The simplest way is configuring an H2 database in memory so you don't have to actually put up some external database engine
Beware in case you use specific features of your specific database engine (the production one, not the testing one, which would be h2) that are not SQL standard, or at least check that are supported in H2.
Baeldung has a great article about the specificansteps tondo exactly this: https://www.baeldung.com/spring-testing-separate-data-source