Unit testing batch jobs

Spring Batch provides different ways to test a batch job; the whole job, only one step, or just a Tasklet class can be tested.

How to do it…

Follow these steps to unit test batch jobs:

  1. Add the Maven dependency for spring-batch-test in pom.xml:
    <dependency>
      <groupId>org.springframework.batch</groupId>
      <artifactId>spring-batch-test</artifactId>
      <version>3.0.2.RELEASE</version>
    </dependency>
  2. In the unit test class, if using JUnit, load the Spring Batch configuration class like this:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = {BatchConfig.class})
    public class BatchJob1Test {
    ...
  3. If using TestNG, load the Spring Batch configuration class as follows:
    @ContextConfiguration(classes = {BatchConfig.class}) public ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.