Java Reference
In-Depth Information
@ContextConfiguration(locations = { "/test-context.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
public class CarFileReaderIntegrationTest {
private ItemReader<String> carFileReader;
public StepExecution getStepExecution() {
JobParameters jobParams = new JobParametersBuilder().addString(
"carFile", "classpath:/data/carfile.txt").toJobParameters();
return MetaDataInstanceFactory.createStepExecution(jobParams);
public void testCarFileReader() throws Exception {
StepExecution execution = getStepExecution();
Integer readCount =
StepScopeTestUtils.doInStepScope(execution, new Callable<Integer>() {
public Integer call() throws Exception {
((ItemStream) carFileReader).open(new ExecutionContext());
int i = 0;
while( != null) { i++; }
return i;
assertEquals(readCount.intValue(), 5);
The StepScopeTestUtils object contains a single utility method called doInStepScope , as shown in
Listing 12-16. This method accepts a StepExecution and a Callable implementation. When the test is
executed, StepScopeTestUtils addresses the runtime injection, as Spring Batch normally would, and
then executes the Callable implementation, returning the result. In this case, the Callable
implementation counts the number of records in your test file and returns the number for you to
validate that it's correct.
Integration tests of this nature can be very useful to test custom developed components such as
custom ItemReaders and ItemWriters. However, as you can see, the value of testing Spring Batch's own
components is minimal at best. Rest assured, it has test coverage for these very things. Instead, it may be
more useful to test your batch jobs by executing an entire step. The next section looks at the tools Spring
Batch provides to make that happen.
Search WWH ::

Custom Search