Class JobOperatorTestUtils
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 with the name batch.random.
This will ensure restartability when no parameters are provided.
- Since:
- 6.0
- Author:
- Mahmoud Ben Hassine
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringName of the single-step job surrounding steps when tested individuallyprotected org.springframework.batch.core.launch.JobOperatorFields inherited from class JobLauncherTestUtils
job, jobLauncher, jobRepository, logger, secureRandom, stepRunner -
Constructor Summary
ConstructorsConstructorDescriptionJobOperatorTestUtils(org.springframework.batch.core.launch.JobOperator jobOperator, org.springframework.batch.core.repository.JobRepository jobRepository) Create a new instance ofJobOperatorTestUtilswith the provided job repository and job operator. -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.batch.core.job.JobgetJob()Get the job that can be operated by this utility.org.springframework.batch.core.launch.JobOperatorGet the job operator used by this utility.org.springframework.batch.core.repository.JobRepositoryGet the job repository used by this utility.org.springframework.batch.core.job.parameters.JobParametersorg.springframework.batch.core.job.parameters.JobParametersBuildervoidsetJob(org.springframework.batch.core.job.Job job) Set the job that can be operated by this utility.voidsetJobOperator(org.springframework.batch.core.launch.JobOperator jobOperator) Set the job operator to be used by this utility.voidsetJobRepository(org.springframework.batch.core.repository.JobRepository jobRepository) Set the job repository to be used by this utility.org.springframework.batch.core.job.JobExecutionstartJob()Start the entire job, including all steps, with a set of unique random job parameters.org.springframework.batch.core.job.JobExecutionstartJob(org.springframework.batch.core.job.parameters.JobParameters jobParameters) Start the entire job, including all steps, with the provided set of job parameters.org.springframework.batch.core.job.JobExecutionStart just the specified step in a surrounding single-step job of typeSimpleJobnamedJOB_NAME.org.springframework.batch.core.job.JobExecutionstartStep(String stepName, org.springframework.batch.core.job.parameters.JobParameters jobParameters, org.springframework.batch.infrastructure.item.ExecutionContext jobExecutionContext) Extract the step from the injected job and start it in a surrounding single-step job of typeSimpleJobnamedJOB_NAME.org.springframework.batch.core.job.JobExecutionstartStep(org.springframework.batch.core.step.Step step) Start just the specified step with a unique set of job parameters in a surrounding single-step job of typeSimpleJobnamedStepRunner.JOB_NAME.org.springframework.batch.core.job.JobExecutionstartStep(org.springframework.batch.core.step.Step step, org.springframework.batch.core.job.parameters.JobParameters jobParameters, org.springframework.batch.infrastructure.item.ExecutionContext jobExecutionContext) Start just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.JOB_NAME.Methods inherited from class JobLauncherTestUtils
getJobLauncher, getStepRunner, launchJob, launchJob, launchStep, launchStep, launchStep, launchStep, setJobLauncher
-
Field Details
-
JOB_NAME
Name of the single-step job surrounding steps when tested individually- See Also:
-
jobOperator
protected org.springframework.batch.core.launch.JobOperator jobOperator
-
-
Constructor Details
-
JobOperatorTestUtils
public JobOperatorTestUtils(org.springframework.batch.core.launch.JobOperator jobOperator, org.springframework.batch.core.repository.JobRepository jobRepository) Create a new instance ofJobOperatorTestUtilswith the provided job repository and job operator.- Parameters:
jobOperator- to use to start jobs and stepsjobRepository- to use to access job metadata
-
-
Method Details
-
setJob
public void setJob(org.springframework.batch.core.job.Job job) Set the job that can be operated by this utility.- Overrides:
setJobin classJobLauncherTestUtils- Parameters:
job- the job to test
-
getJob
public org.springframework.batch.core.job.Job getJob()Get the job that can be operated by this utility.- Overrides:
getJobin classJobLauncherTestUtils- Returns:
- the job under test
-
setJobOperator
public void setJobOperator(org.springframework.batch.core.launch.JobOperator jobOperator) Set the job operator to be used by this utility.- Parameters:
jobOperator- the job operator to use to start jobs and steps
-
getJobOperator
public org.springframework.batch.core.launch.JobOperator getJobOperator()Get the job operator used by this utility.- Returns:
- the job operator
-
setJobRepository
public void setJobRepository(org.springframework.batch.core.repository.JobRepository jobRepository) Set the job repository to be used by this utility.- Overrides:
setJobRepositoryin classJobLauncherTestUtils- Parameters:
jobRepository- the job repository to use to access job metadata
-
getJobRepository
public org.springframework.batch.core.repository.JobRepository getJobRepository()Get the job repository used by this utility.- Overrides:
getJobRepositoryin classJobLauncherTestUtils- Returns:
- the job repository
-
startJob
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 org.springframework.batch.core.job.JobExecution startJob(org.springframework.batch.core.job.parameters.JobParameters jobParameters) throws Exception Start the entire job, including all steps, with the provided set of job parameters.- Parameters:
jobParameters- instance ofJobParameters.- Returns:
- JobExecution, so that the test can validate the exit status
- Throws:
Exception- thrown if error occurs launching the job.
-
startStep
Start just the specified step in a surrounding single-step job of typeSimpleJobnamedJOB_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 org.springframework.batch.core.job.JobExecution startStep(String stepName, org.springframework.batch.core.job.parameters.JobParameters jobParameters, org.springframework.batch.infrastructure.item.ExecutionContext jobExecutionContext) Extract the step from the injected job and start it in a surrounding single-step job of typeSimpleJobnamedJOB_NAME. An IllegalStateException is thrown if there is no Step with the given name.- Parameters:
stepName- The name of the step to startjobParameters- The JobParameters to use during the startjobExecutionContext- An ExecutionContext whose values will be loaded into the Job ExecutionContext before starting the step.- Returns:
- JobExecution
-
startStep
public org.springframework.batch.core.job.JobExecution startStep(org.springframework.batch.core.step.Step step) Start just the specified step with a unique set of job parameters in a surrounding single-step job of typeSimpleJobnamedStepRunner.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 org.springframework.batch.core.job.JobExecution startStep(org.springframework.batch.core.step.Step step, org.springframework.batch.core.job.parameters.JobParameters jobParameters, org.springframework.batch.infrastructure.item.ExecutionContext jobExecutionContext) Start just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.JOB_NAME. An IllegalStateException is thrown if there is no Step with the given name.- Parameters:
step- The step to startjobParameters- The JobParameters to use during the startjobExecutionContext- An ExecutionContext whose values will be loaded into the Job ExecutionContext before starting the step.- Returns:
- JobExecution
-
getUniqueJobParameters
public org.springframework.batch.core.job.parameters.JobParameters getUniqueJobParameters()- Overrides:
getUniqueJobParametersin classJobLauncherTestUtils- Returns:
- a new
JobParametersobject containing only one parameter namedbatch.randomwith a random number of typelong, to ensure that the job instance will be unique.
-
getUniqueJobParametersBuilder
public org.springframework.batch.core.job.parameters.JobParametersBuilder getUniqueJobParametersBuilder()- Overrides:
getUniqueJobParametersBuilderin classJobLauncherTestUtils- Returns:
- a new
JobParametersBuilderobject containing only one parameter namedbatch.randomwith a random number of typelong, to ensure that the job instance will be unique.
-