Java Reference
In-Depth Information
Listing 12-20. Testing carJob
@Test
public void testCarJob() throws Exception {
JobExecution execution = jobLauncherUtils.launchJob(getParams());
assertEquals(ExitStatus.COMPLETED, execution.getExitStatus());
StepExecution stepExecution =
execution.getStepExecutions().iterator().next();
assertEquals(ExitStatus.COMPLETED, stepExecution.getExitStatus());
assertEquals(5, stepExecution.getReadCount());
assertEquals(5, stepExecution.getWriteCount());
assertFileEquals(new ClassPathResource(INPUT_FILE),
new FileSystemResource(OUTPUT_FILE));
}
private JobParameters getParams() {
return new JobParametersBuilder().addString("carFile", INPUT_FILE)
.addString("outputFile", "file:/" + OUTPUT_FILE)
.toJobParameters();
}
As Listing 12-20 shows, executing your job requires only a single line of code. From there, you're
able to verify the ExitStatus of the job, any steps in the job, and the read and write count for those steps,
and also assert that the results of the job match what you expected.
Summary
From unit-testing a single method in any component in your system all the way to executing batch jobs
programmatically, you've covered the vast majority of testing scenarios you may encounter as a batch
programmer. This chapter began with an overview of the JUnit test framework and the Mockito mock-
object frameworks for unit testing. You then explored integration testing using the classes and
annotations provided by Spring, including executing tests in transactions. Finally, you looked at Spring
Batch-specific testing by executing components that are defined in the step scope, individual steps in
jobs, and finally the entire job.
 
Search WWH ::




Custom Search