Package org.springframework.batch.test
Class StepScopeTestExecutionListener
- java.lang.Object
- org.springframework.batch.test.StepScopeTestExecutionListener
- All Implemented Interfaces:
org.springframework.test.context.TestExecutionListener
public class StepScopeTestExecutionListener extends java.lang.Object implements org.springframework.test.context.TestExecutionListener
ATestExecutionListener
that sets up step-scope context for dependency injection into unit tests. AStepContext
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 aStepExecution
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, Chris Schaefer
Constructor Summary
Constructors Constructor Description StepScopeTestExecutionListener()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
afterTestClass(org.springframework.test.context.TestContext testContext)
void
afterTestMethod(org.springframework.test.context.TestContext testContext)
void
beforeTestClass(org.springframework.test.context.TestContext testContext)
void
beforeTestMethod(org.springframework.test.context.TestContext testContext)
protected StepExecution
getStepExecution(org.springframework.test.context.TestContext testContext)
Discover aStepExecution
as a field in the test case or create one if none is available.void
prepareTestInstance(org.springframework.test.context.TestContext testContext)
Set up aStepExecution
as a test context attribute.
Method Detail
prepareTestInstance
public void prepareTestInstance(org.springframework.test.context.TestContext testContext) throws java.lang.Exception
Set up aStepExecution
as a test context attribute.- Specified by:
prepareTestInstance
in interfaceorg.springframework.test.context.TestExecutionListener
- Parameters:
testContext
- the current test context- Throws:
java.lang.Exception
- if there is a problem- See Also:
TestExecutionListener.prepareTestInstance(TestContext)
beforeTestMethod
public void beforeTestMethod(org.springframework.test.context.TestContext testContext) throws java.lang.Exception
- Specified by:
beforeTestMethod
in interfaceorg.springframework.test.context.TestExecutionListener
- Parameters:
testContext
- the current test context- Throws:
java.lang.Exception
- if there is a problem- See Also:
TestExecutionListener.beforeTestMethod(TestContext)
afterTestMethod
public void afterTestMethod(org.springframework.test.context.TestContext testContext) throws java.lang.Exception
- Specified by:
afterTestMethod
in interfaceorg.springframework.test.context.TestExecutionListener
- Parameters:
testContext
- the current test context- Throws:
java.lang.Exception
- if there is a problem- See Also:
TestExecutionListener.afterTestMethod(TestContext)
afterTestClass
public void afterTestClass(org.springframework.test.context.TestContext testContext) throws java.lang.Exception
- Specified by:
afterTestClass
in interfaceorg.springframework.test.context.TestExecutionListener
- Throws:
java.lang.Exception
beforeTestClass
public void beforeTestClass(org.springframework.test.context.TestContext testContext) throws java.lang.Exception
- Specified by:
beforeTestClass
in interfaceorg.springframework.test.context.TestExecutionListener
- Throws:
java.lang.Exception
getStepExecution
protected StepExecution getStepExecution(org.springframework.test.context.TestContext testContext)
Discover aStepExecution
as a field in the test case or create one if none is available.- Parameters:
testContext
- the current test context- Returns:
- a
StepExecution