public class JobScopeTestExecutionListener
extends java.lang.Object
implements org.springframework.test.context.TestExecutionListener
TestExecutionListener
that sets up job-scope context for
dependency injection into unit tests. A JobContext
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 JobExecution
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, JobScopeTestExecutionListener.class }) @RunWith(SpringJUnit4ClassRunner.class) public class JobScopeTestExecutionListenerIntegrationTests { // A job-scoped dependency configured in the ApplicationContext @Autowired private ItemReader<String> reader; public JobExecution getJobExecution() { JobExecution execution = MetaDataInstanceFactory.createJobExecution(); execution.getExecutionContext().putString("foo", "bar"); return execution; } @Test public void testJobScopedReader() { // Job context is active here so the reader can be used, // and the job execution context will contain foo=bar... assertNotNull(reader.read()); } }
Constructor and Description |
---|
JobScopeTestExecutionListener() |
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 JobExecution |
getJobExecution(org.springframework.test.context.TestContext testContext)
Discover a
JobExecution 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
JobExecution as a test context attribute. |
public void prepareTestInstance(org.springframework.test.context.TestContext testContext) throws java.lang.Exception
JobExecution
as a test context attribute.prepareTestInstance
in interface org.springframework.test.context.TestExecutionListener
testContext
- 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.TestExecutionListener
testContext
- 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.TestExecutionListener
testContext
- 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.TestExecutionListener
java.lang.Exception
public void beforeTestClass(org.springframework.test.context.TestContext testContext) throws java.lang.Exception
beforeTestClass
in interface org.springframework.test.context.TestExecutionListener
java.lang.Exception
protected JobExecution getJobExecution(org.springframework.test.context.TestContext testContext)
JobExecution
as a field in the test case or create
one if none is available.testContext
- the current test contextJobExecution