org.springframework.batch.test
Class StepScopeTestExecutionListener
java.lang.Object
org.springframework.batch.test.StepScopeTestExecutionListener
- All Implemented Interfaces:
- TestExecutionListener
public class StepScopeTestExecutionListener
- extends Object
- implements TestExecutionListener
A TestExecutionListener
that sets up step-scope context for
dependency injection into unit tests. A StepContext
will be created
for the duration of a test method and made available to any dependencies that
are injected. The default behaviour is just to create a StepExecution
with fixed properties. Alternatively it can be provided by the test case as a
factory methods returning the correct type. Example:
@ContextConfiguration
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class StepScopeTestExecutionListenerIntegrationTests {
// A step-scoped dependency configured in the ApplicationContext
@Autowired
private ItemReader<String> reader;
public StepExecution getStepExecution() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getExecutionContext().putString("foo", "bar");
return execution;
}
@Test
public void testStepScopedReader() {
// Step context is active here so the reader can be used,
// and the step execution context will contain foo=bar...
assertNotNull(reader.read());
}
}
- Author:
- Dave Syer
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
StepScopeTestExecutionListener
public StepScopeTestExecutionListener()
prepareTestInstance
public void prepareTestInstance(TestContext testContext)
throws Exception
- Set up a
StepExecution
as a test context attribute.
- Specified by:
prepareTestInstance
in interface TestExecutionListener
- Parameters:
testContext
- the current test context
- Throws:
Exception
- if there is a problem- See Also:
TestExecutionListener.prepareTestInstance(TestContext)
beforeTestMethod
public void beforeTestMethod(TestContext testContext)
throws Exception
- Specified by:
beforeTestMethod
in interface TestExecutionListener
- Parameters:
testContext
- the current test context
- Throws:
Exception
- if there is a problem- See Also:
TestExecutionListener.beforeTestMethod(TestContext)
afterTestMethod
public void afterTestMethod(TestContext testContext)
throws Exception
- Specified by:
afterTestMethod
in interface TestExecutionListener
- Parameters:
testContext
- the current test context
- Throws:
Exception
- if there is a problem- See Also:
TestExecutionListener.afterTestMethod(TestContext)
afterTestClass
public void afterTestClass(TestContext testContext)
throws Exception
- Throws:
Exception
beforeTestClass
public void beforeTestClass(TestContext testContext)
throws Exception
- Throws:
Exception
getStepExecution
protected StepExecution getStepExecution(TestContext testContext)
- Discover a
StepExecution
as a field in the test case or create
one if none is available.
- Parameters:
testContext
- the current test context
- Returns:
- a
StepExecution
Copyright © 2013 SpringSource. All Rights Reserved.