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 Step
s individually from a
SimpleJob
FlowJob
. By starting Step
s 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 Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Name of the single-step job surrounding steps when tested individuallyprotected JobOperator
Fields inherited from class org.springframework.batch.test.JobLauncherTestUtils
job, jobLauncher, jobRepository, logger, secureRandom, stepRunner
-
Constructor Summary
ConstructorsConstructorDescriptionJobOperatorTestUtils
(JobOperator jobOperator, JobRepository jobRepository) Create a new instance ofJobOperatorTestUtils
with the provided job repository and job operator. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Set the job that can be operated by this utility.void
setJobOperator
(JobOperator jobOperator) Set the job operator to be used by this utility.void
setJobRepository
(JobRepository jobRepository) Set the job repository to be used by this utility.startJob()
Start the entire job, including all steps, with a set of unique random job parameters.startJob
(JobParameters jobParameters) Start the entire job, including all steps, with the provided set of job parameters.startStep
(String stepName, JobParameters jobParameters, ExecutionContext jobExecutionContext) Start just the specified step with a unique set of job parameters in a surrounding single-step job of typeSimpleJob
namedStepRunner.JOB_NAME
.startStep
(Step step, JobParameters jobParameters, ExecutionContext jobExecutionContext) Start just the specified step in a surrounding single-step job of typeSimpleJob
namedStepRunner.JOB_NAME
.Methods inherited from class org.springframework.batch.test.JobLauncherTestUtils
getJob, getJobLauncher, getJobRepository, 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
-
-
Constructor Details
-
JobOperatorTestUtils
Create a new instance ofJobOperatorTestUtils
with 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
Set the job that can be operated by this utility.- Overrides:
setJob
in classJobLauncherTestUtils
- Parameters:
job
- the job to test
-
setJobOperator
Set the job operator to be used by this utility.- Parameters:
jobOperator
- the job operator to use to start jobs and steps
-
setJobRepository
Set the job repository to be used by this utility.- Overrides:
setJobRepository
in classJobLauncherTestUtils
- Parameters:
jobRepository
- the job repository to use to access job metadata
-
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
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 typeSimpleJob
namedJOB_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 typeSimpleJob
namedJOB_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
Start just the specified step with a unique set of job parameters in a surrounding single-step job of typeSimpleJob
namedStepRunner.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 typeSimpleJob
namedStepRunner.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
- Overrides:
getUniqueJobParameters
in classJobLauncherTestUtils
- Returns:
- a new
JobParameters
object containing only a parameter with a random number of typelong
, to ensure that the job instance will be unique.
-
getUniqueJobParametersBuilder
- Overrides:
getUniqueJobParametersBuilder
in classJobLauncherTestUtils
- Returns:
- a new
JobParametersBuilder
object containing only a parameter with a random number of typelong
, to ensure that the job instance will be unique.
-