Java Reference
In-Depth Information
order of operations between components of a system is correct can provide considerable security when
you're dealing with complex systems.
The final piece of testing with Spring Batch is testing the Spring Batch components themselves.
ItemReaders, steps, and even entire jobs can be tested with the tools provided by Spring. The final
section of this chapter looks at how to use those components and test pieces of your batch process.
Testing Spring Batch
Although the ability to test components like a DAO or a service is definitely needed when you're working
with robust batch jobs, working with the Spring Batch framework introduces a collection of additional
complexities into your code that need to be addressed in order to build a robust test suite. This section
looks at how to handle testing Spring Batch-specific components, including elements that depend on
custom scopes, Spring Batch steps, and even complete jobs.
Testing Step-Scoped Beans
As you've seen in many examples throughout this topic, the step scope defined by Spring Batch for your
Spring Beans is a very helpful tool. However, when you're writing integration tests for components that
use the step scope, you run into an issue: if you're executing those components outside the scope of a
step, how do those dependencies get resolved? In this section, you look at the two ways Spring Batch
offers to simulate that a bean is being executed in the scope of a step.
You've seen in the past how using the step scope allows Spring Batch to inject runtime values from
the job and/or step context into your beans. Previous examples include the injection of an input or
output file name, or criteria for a particular database query. In each of those examples, Spring Batch
obtains the values from the JobExecution or the StepExecution. If you aren't running the job in a step,
you don't have either of those executions. Spring Batch provides two different ways to emulate the
execution in a step so that those values can be injected. The first approach uses a TestExecutionListener.
TestExecutionListener is a Spring API that allows you to define things to occur before or after a test
method. Unlike using JUnit's @Before and @After annotations, using Spring's TestExecutionListener
allows you to inject behavior across all the methods in a test case in a more reusable way. Although
Spring provides three useful implementations of the TestExecutionListener interface
(DependencyInjectionTestExecutionListener, DirtiesContextTestExecutionListener, and
TransactionalTestExecutionListener), Spring Batch provides one that handles what you're looking for:
StepScopeTestExecutionListener.
StepScopeTestExecutionListener provides two features you need. First, it uses a factory method
from your test case to obtain a StepExecution and uses the one returned as the context for the current
test method. Second, it provides a StepContext for the life of each test method. Figure 12-2 shows the
flow of a test being executed using StepScopeTestExecutionListener.
SpringJUnit4ClassRunner
StepScopeTestExecutionListener
OurTest
@Before
beforeTestMethod
getStepExecution
@Test
afterTestMethod
Close step management
@After
Figure 12-2. Test execution using StepScopeTestExecutionListener
 
 
Search WWH ::




Custom Search