2

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?

TheStranger
  • 1,387
  • 1
  • 13
  • 35
  • 1
    According to an old song (and movie), Grease is the word. In your case, however, mocking is the word (as in to mock a service). here's one place to start: https://www.baeldung.com/mockito-series – Stultuske Dec 22 '21 at 14:28
  • 1
    You can use Test containers(https://www.testcontainers.org/test_framework_integration/junit_5/) which is very good for integration testing. Which database are you using, I can post a example for that. – ata Dec 22 '21 at 14:31
  • @Akbar I'm using MongoDB – TheStranger Dec 22 '21 at 14:39
  • 1
    @Ben Look at this example, it is using mongodb and test containers. https://rieckpil.de/mongodb-testcontainers-setup-for-datamongotest/ – ata Dec 22 '21 at 14:43
  • @Akbar Okay, I will try that. That looks exactly like what i was searching for. Thank you :) – TheStranger Dec 22 '21 at 14:46
  • I don't know how somehow I bypassed the word MongoDB. I deleted my answer. May be this other question can help you https://stackoverflow.com/questions/31970690/how-to-unit-test-a-spring-boot-mongorepository – gmanjon Dec 24 '21 at 01:44

2 Answers2

0

Here is an example for testing JPA Queries with Spring Boot and @DataJpaTest

https://reflectoring.io/spring-boot-data-jpa-test/

Souhail HARRATI
  • 303
  • 2
  • 7
0

If you wanna "unit" test your controller You can use @WebMvcTest and just mock your service calls from controller. Find out here a pretty good getting started with @WebMvcTest https://reflectoring.io/spring-boot-web-controller-test/

If you really need to do integration tests You can use embedded mongoDb database and @SpringBootTest in combination with @DataMongoTest annotation (Find out here how to do that. @SpringBootTest will load your applicationContext and @DataMongoTest will autoconfigure all you need for seting up the embedded mongoDb database.

de.flapdoodle.embed.mongo provides embedded MongoDB for integration tests. So you need to add its dependency.

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>

If you wanna prefill your database you can create a bean in a spring configuration class

@Bean
  public Boolean preFill(MongoTemplate mongoTemplate) throws IOException {
     JSONParser parser = getJsonParser();
      List dbObjects = /* Init your objects to insert here */;

      for (Object dbObject : dbObjects) {
        Document document = new Document(parser.parseJson(JSON.serialize(dbObject)));
        mongoTemplate.insert(document, dbObject.getKey());
      }
    });
    return true;
  }
  
  private JSONParser getJsonParser() {
    JsonParserFactory factory= JsonParserFactory.getInstance();
    return factory.newJsonParser();
  }
soung
  • 1,411
  • 16
  • 33