Skip to content

Spring Batch Integration Test with MongoDB

The documentation here uses jdbc template. https://docs.spring.io/spring-batch/docs/4.1.x/reference/html/testing.html#testing

I would like to ask on how I could write an integration test with Spring Batch using MongoDB? Preferably, if you guys could provide me a concrete example.

I am using these dependencies for your information

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
  </dependency>
  <dependency>
         <groupId>de.flapdoodle.embed</groupId>
         <artifactId>de.flapdoodle.embed.mongo</artifactId>
         <scope>test</scope>
  </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <version>${spring.batch.version}</version>
            <scope>test</scope>
        </dependency>

Answer

If only I could find a spring batch example using mongodb

You can find an example of a job reading/writing data from/to MongoDB here: https://github.com/spring-projects/spring-batch/tree/master/spring-batch-samples#mongodb-sample.

The code of the example is here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-samples/src/main/java/org/springframework/batch/sample/mongodb/MongoDBSampleApp.java.

If you are planning to write an integration test against MongoDB, you can use flapdoodle.embed.mongo or testcontainers.