public class StepScopeTestExecutionListener
extends java.lang.Object
implements org.springframework.test.context.TestExecutionListener
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());
}
}
| Constructor and Description |
|---|
StepScopeTestExecutionListener() |
| Modifier and Type | Method and 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 org.springframework.batch.core.StepExecution |
getStepExecution(org.springframework.test.context.TestContext testContext)
Discover a
StepExecution as a field in the test case or create
one if none is available. |
void |
prepareTestInstance(org.springframework.test.context.TestContext testContext)
Set up a
StepExecution as a test context attribute. |
public void prepareTestInstance(org.springframework.test.context.TestContext testContext)
throws java.lang.Exception
StepExecution as a test context attribute.prepareTestInstance in interface org.springframework.test.context.TestExecutionListenertestContext - the current test contextjava.lang.Exception - if there is a problemTestExecutionListener.prepareTestInstance(TestContext)public void beforeTestMethod(org.springframework.test.context.TestContext testContext)
throws java.lang.Exception
beforeTestMethod in interface org.springframework.test.context.TestExecutionListenertestContext - the current test contextjava.lang.Exception - if there is a problemTestExecutionListener.beforeTestMethod(TestContext)public void afterTestMethod(org.springframework.test.context.TestContext testContext)
throws java.lang.Exception
afterTestMethod in interface org.springframework.test.context.TestExecutionListenertestContext - the current test contextjava.lang.Exception - if there is a problemTestExecutionListener.afterTestMethod(TestContext)public void afterTestClass(org.springframework.test.context.TestContext testContext)
throws java.lang.Exception
afterTestClass in interface org.springframework.test.context.TestExecutionListenerjava.lang.Exceptionpublic void beforeTestClass(org.springframework.test.context.TestContext testContext)
throws java.lang.Exception
beforeTestClass in interface org.springframework.test.context.TestExecutionListenerjava.lang.Exceptionprotected org.springframework.batch.core.StepExecution getStepExecution(org.springframework.test.context.TestContext testContext)
StepExecution as a field in the test case or create
one if none is available.testContext - the current test contextStepExecution