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:
Add the Maven dependency for
spring-batch-test
inpom.xml
:<dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <version>3.0.2.RELEASE</version> </dependency>
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 { ...
If using TestNG, load the Spring Batch configuration class as follows:
@ContextConfiguration(classes = {BatchConfig.class}) public class BatchJob1Test extends AbstractTestNGSpringContextTests { ...
Add an autowired
JobLauncherTestUtils
field:@Autowired private JobLauncherTestUtils jobLauncherTestUtils;
This is how you can...