Java Reference
In-Depth Information
carJob uses a FlatFileItemReader to read in a file whose location is passed in at runtime via the job
parameters. It passes the input to a FlatFileItemWriter that writes the output to a file whose location is
also provided at runtime via the job parameters. These two components are used in a single step to
make up your job.
To execute this step via an integration test, the test is structured very much like
CarFileReaderIntegrationTest shown in Listing 12-17. You use annotations to tell Spring where to find
your context configuration files and what to inject, and you configure the test to be executed via
SpringJUnit4ClassRunner. You even build your own JobParameters object to pass in to the job. But that
is where the similarities end.
To execute a step, you use another utility provided by Spring Batch: JobLauncherTestUtils . This
utility class provides a number of methods for launching both steps and jobs in a variety of ways (with
parameters, without parameters, with an execution context, without, and so on). When you execute a
step, you receive the JobExecution back, in which you can inspect what happened in the job. Listing 12-
18 has the code to test carProcessingStep .
Listing 12-18. Testing carProcessingStep
package com.apress.springbatch.chapter12;
import static org.junit.Assert.assertEquals;
import static org.springframework.batch.test.AssertFile.assertFileEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
@ContextConfiguration(locations = { "/test-context.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class CarProcessingStepIntegrationTest {
private static final String OUTPUT_FILE = "/"
+ System.getProperty("java.io.tmpdir") + "carOutput.txt";
private static final String INPUT_FILE = "/data/carfile.txt";
@Autowired
private JobLauncherTestUtils jobLauncherUtils;
 
Search WWH ::




Custom Search