Class JobOperatorTestUtils

java.lang.Object
org.springframework.batch.test.JobLauncherTestUtils
org.springframework.batch.test.JobOperatorTestUtils

public class JobOperatorTestUtils extends JobLauncherTestUtils

Utility class for testing batch jobs. It provides methods for starting an entire AbstractJob, allowing for end-to-end testing of individual steps, without having to run every step in the job. Any test classes using this utility can set up an instance in the ApplicationContext as part of the Spring test framework. The test context must contain batch infrastructure beans (ie a JobRepository and a JobOperator) as well as the job under test. The job under test will be autowired into this utility.

This class also provides the ability to run Steps individually from a SimpleJob FlowJob. By starting Steps within a Job on their own, end-to-end testing of individual steps can be performed without having to run every step in the job.

It should be noted that using any of the methods that don't contain JobParameters in their signature, will result in one being created with a random number of type long as a parameter. This will ensure restartability when no parameters are provided.

Since:
6.0
Author:
Mahmoud Ben Hassine
  • Field Details

    • JOB_NAME

      public static final String JOB_NAME
      Name of the single-step job surrounding steps when tested individually
      See Also:
    • jobOperator

      protected JobOperator jobOperator
  • Constructor Details

    • JobOperatorTestUtils

      public JobOperatorTestUtils(JobOperator jobOperator, JobRepository jobRepository)
      Create a new instance of JobOperatorTestUtils with the provided job repository and job operator.
      Parameters:
      jobOperator - to use to start jobs and steps
      jobRepository - to use to access job metadata
  • Method Details

    • setJob

      public void setJob(Job job)
      Set the job that can be operated by this utility.
      Overrides:
      setJob in class JobLauncherTestUtils
      Parameters:
      job - the job to test
    • setJobOperator

      public void setJobOperator(JobOperator jobOperator)
      Set the job operator to be used by this utility.
      Parameters:
      jobOperator - the job operator to use to start jobs and steps
    • setJobRepository

      public void setJobRepository(JobRepository jobRepository)
      Set the job repository to be used by this utility.
      Overrides:
      setJobRepository in class JobLauncherTestUtils
      Parameters:
      jobRepository - the job repository to use to access job metadata
    • startJob

      public JobExecution startJob() throws Exception
      Start the entire job, including all steps, with a set of unique random job parameters.
      Returns:
      JobExecution, so that the test can validate the exit status
      Throws:
      Exception - thrown if error occurs launching the job.
    • startJob

      public JobExecution startJob(JobParameters jobParameters) throws Exception
      Start the entire job, including all steps, with the provided set of job parameters.
      Parameters:
      jobParameters - instance of JobParameters.
      Returns:
      JobExecution, so that the test can validate the exit status
      Throws:
      Exception - thrown if error occurs launching the job.
    • startStep

      public JobExecution startStep(String stepName)
      Start just the specified step in a surrounding single-step job of type SimpleJob named JOB_NAME. A unique set of JobParameters will automatically be generated. An IllegalStateException is thrown if there is no Step with the given name.
      Parameters:
      stepName - The name of the step to launch
      Returns:
      JobExecution
    • startStep

      public JobExecution startStep(String stepName, JobParameters jobParameters, ExecutionContext jobExecutionContext)
      Extract the step from the injected job and start it in a surrounding single-step job of type SimpleJob named JOB_NAME. An IllegalStateException is thrown if there is no Step with the given name.
      Parameters:
      stepName - The name of the step to start
      jobParameters - The JobParameters to use during the start
      jobExecutionContext - An ExecutionContext whose values will be loaded into the Job ExecutionContext before starting the step.
      Returns:
      JobExecution
    • startStep

      public JobExecution startStep(Step step)
      Start just the specified step with a unique set of job parameters in a surrounding single-step job of type SimpleJob named StepRunner.JOB_NAME. An IllegalStateException is thrown if there is no Step with the given name.
      Parameters:
      step - The step to start
      Returns:
      JobExecution
    • startStep

      public JobExecution startStep(Step step, JobParameters jobParameters, ExecutionContext jobExecutionContext)
      Start just the specified step in a surrounding single-step job of type SimpleJob named StepRunner.JOB_NAME. An IllegalStateException is thrown if there is no Step with the given name.
      Parameters:
      step - The step to start
      jobParameters - The JobParameters to use during the start
      jobExecutionContext - An ExecutionContext whose values will be loaded into the Job ExecutionContext before starting the step.
      Returns:
      JobExecution
    • getUniqueJobParameters

      public JobParameters getUniqueJobParameters()
      Overrides:
      getUniqueJobParameters in class JobLauncherTestUtils
      Returns:
      a new JobParameters object containing only a parameter with a random number of type long, to ensure that the job instance will be unique.
    • getUniqueJobParametersBuilder

      public JobParametersBuilder getUniqueJobParametersBuilder()
      Overrides:
      getUniqueJobParametersBuilder in class JobLauncherTestUtils
      Returns:
      a new JobParametersBuilder object containing only a parameter with a random number of type long, to ensure that the job instance will be unique.