Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 class BatchJob1Test extends AbstractTestNGSpringContextTests {
    ...
  4. Add an autowired JobLauncherTestUtils field:

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;
  5. This is how you can...