Book Image

SPRING COOKBOOK

By : Jerome Jaglale, Yilmaz
Book Image

SPRING COOKBOOK

By: Jerome Jaglale, Yilmaz

Overview of this book

This book is for you if you have some experience with Java and web development (not necessarily in Java) and want to become proficient quickly with Spring.
Table of Contents (14 chapters)
13
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...