Java Reference
In-Depth Information
As you can see, the factory method you create in the test case ( getStepExecution ) is called before
each test method to obtain a new StepExecution. If there is no factory method, Spring Batch uses a
default StepExecution.
To test this, you configure a FlatFileItemReader to obtain the location of the file to read from the
jobParameters . First let's look at the configuration of the ItemReader to test and the resource you use
(see Listing 12-13).
Listing 12-13. Car File's ItemReader Configuration
<beans:bean id="carFileReader"
class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<beans:property name="resource" value="#{jobParameters[carFile]}"/>
<beans:property name="lineMapper">
<beans:bean
class="org.springframework.batch.item.file.mapping.PassThroughLineMapper"/>
</beans:property>
</beans:bean>
Listing 12-13 is a simple FlatFileItemReader that is configured to use the step scope so you can set
the name of the input file at runtime via the job parameters. To test this ItemReader, you begin the same
way you did with TickerDaoIntegrationTest by telling Spring the location of the context configuration
using the @ContextConfiguration annotation. However, with this test, you expand the use of annotations
to include the following:
@TestExecutionListeners : Using DependencyInjectionTestExecutionListener
eliminates the need to extend any specific class from Spring to obtain the ability to
have your beans wired and injected via Spring. StepScopeTestExecutionListener
calls getStepExecution to obtain a StepExecution complete with any parameters
that Spring Batch would inject for you.
@RunWith : The listeners in the previous item are a Spring concept unknown to
JUnit. So, you need to use Spring's test runner instead of the standard JUnit one.
For this test to work, you need a test file for the ItemReader to read. For test purposes, it's a good
idea to include any test files in the <PROJECT>/src/test/resources directory so they're available wherever
the tests are run. In this case, you include a CSV file named carfile.csv in the directory
<project_home>/src/test/resources/data . The contents of the file are shown in Listing 12-14.
Listing 12-14. carFile.csv
1987,Nissan,Sentra
1991,Plymouth,Laser
1995,Mercury,Cougar
2000,Infiniti,QX4
2001,Infiniti,QX4
With the environment configured using the same contexts you used in the previous example and
the new ItemReader configured as well as a sample input file supplied for testing, you can put together
the integration test. This integration test loops through the file, reading each line until the file is
complete (five times, in this case). You verify that each record is returned until the final (sixth) call to the
reader is null , indicating the input has been exhausted. Listing 12-15 shows the integration test.
 
Search WWH ::




Custom Search