Class JobScopeTestExecutionListener

java.lang.Object
org.springframework.batch.test.JobScopeTestExecutionListener
All Implemented Interfaces:
org.springframework.test.context.TestExecutionListener

public class JobScopeTestExecutionListener extends Object implements org.springframework.test.context.TestExecutionListener
A 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 })
@SpringJUnitConfig
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());
        }

}
Author:
Dave Syer, Jimmy Praet, Mahmoud Ben Hassine, Hyuntae Park
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterTestMethod(org.springframework.test.context.TestContext testContext)
     
    void
    beforeTestMethod(org.springframework.test.context.TestContext testContext)
     
    protected org.springframework.batch.core.job.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.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.test.context.TestExecutionListener

    afterTestClass, afterTestExecution, beforeTestClass, beforeTestExecution
  • Constructor Details

    • JobScopeTestExecutionListener

      public JobScopeTestExecutionListener()
  • Method Details

    • prepareTestInstance

      public void prepareTestInstance(org.springframework.test.context.TestContext testContext)
      Set up a JobExecution as a test context attribute.
      Specified by:
      prepareTestInstance in interface org.springframework.test.context.TestExecutionListener
      Parameters:
      testContext - the current test context
      See Also:
      • TestExecutionListener.prepareTestInstance(TestContext)
    • beforeTestMethod

      public void beforeTestMethod(org.springframework.test.context.TestContext testContext)
      Specified by:
      beforeTestMethod in interface org.springframework.test.context.TestExecutionListener
      Parameters:
      testContext - the current test context
      See Also:
      • TestExecutionListener.beforeTestMethod(TestContext)
    • afterTestMethod

      public void afterTestMethod(org.springframework.test.context.TestContext testContext)
      Specified by:
      afterTestMethod in interface org.springframework.test.context.TestExecutionListener
      Parameters:
      testContext - the current test context
      See Also:
      • TestExecutionListener.afterTestMethod(TestContext)
    • getJobExecution

      protected org.springframework.batch.core.job.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.
      Parameters:
      testContext - the current test context
      Returns:
      a JobExecution