Class JobLauncherTestUtils
Utility class for testing batch jobs. It provides methods for launching 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.
This class also provides the ability to run Steps from a FlowJob or
SimpleJob individually. By launching 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:
- 2.1
- Author:
- Lucas Ward, Dan Garrette, Dave Syer, Mahmoud Ben Hassine
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.batch.core.JobgetJob()org.springframework.batch.core.launch.JobLauncherorg.springframework.batch.core.repository.JobRepositoryprotected StepRunnerConvenient method for subclasses to grab aStepRunnerfor running steps by name.org.springframework.batch.core.JobParametersorg.springframework.batch.core.JobParametersBuilderorg.springframework.batch.core.JobExecutionLaunch the entire job, including all steps.org.springframework.batch.core.JobExecutionlaunchJob(org.springframework.batch.core.JobParameters jobParameters) Launch the entire job, including all stepsorg.springframework.batch.core.JobExecutionlaunchStep(String stepName) Launch just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.JOB_NAME.org.springframework.batch.core.JobExecutionlaunchStep(String stepName, org.springframework.batch.core.JobParameters jobParameters) Launch just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.JOB_NAME.org.springframework.batch.core.JobExecutionlaunchStep(String stepName, org.springframework.batch.core.JobParameters jobParameters, org.springframework.batch.item.ExecutionContext jobExecutionContext) Launch just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.JOB_NAME.org.springframework.batch.core.JobExecutionlaunchStep(String stepName, org.springframework.batch.item.ExecutionContext jobExecutionContext) Launch just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.JOB_NAME.voidsetJob(org.springframework.batch.core.Job job) The Job instance that can be manipulated (e.g. launched) in this utility.voidsetJobLauncher(org.springframework.batch.core.launch.JobLauncher jobLauncher) AJobLauncherinstance that can be used to launch jobs.voidsetJobRepository(org.springframework.batch.core.repository.JobRepository jobRepository) TheJobRepositoryto use for creating newJobExecutioninstances.
-
Field Details
-
logger
protected final org.apache.commons.logging.Log loggerLogger
-
-
Constructor Details
-
JobLauncherTestUtils
public JobLauncherTestUtils()
-
-
Method Details
-
setJob
public void setJob(org.springframework.batch.core.Job job) The Job instance that can be manipulated (e.g. launched) in this utility.- Parameters:
job- theAbstractJobto use
-
setJobRepository
public void setJobRepository(org.springframework.batch.core.repository.JobRepository jobRepository) TheJobRepositoryto use for creating newJobExecutioninstances.- Parameters:
jobRepository- aJobRepository
-
getJobRepository
public org.springframework.batch.core.repository.JobRepository getJobRepository()- Returns:
- the job repository
-
getJob
public org.springframework.batch.core.Job getJob()- Returns:
- the job
-
setJobLauncher
public void setJobLauncher(org.springframework.batch.core.launch.JobLauncher jobLauncher) AJobLauncherinstance that can be used to launch jobs.- Parameters:
jobLauncher- a job launcher
-
getJobLauncher
public org.springframework.batch.core.launch.JobLauncher getJobLauncher()- Returns:
- the job launcher
-
launchJob
Launch the entire job, including all steps.- Returns:
- JobExecution, so that the test can validate the exit status
- Throws:
Exception- thrown if error occurs launching the job.
-
launchJob
public org.springframework.batch.core.JobExecution launchJob(org.springframework.batch.core.JobParameters jobParameters) throws Exception Launch the entire job, including all steps- Parameters:
jobParameters- instance ofJobParameters.- Returns:
- JobExecution, so that the test can validate the exit status
- Throws:
Exception- thrown if error occurs launching the job.
-
getUniqueJobParameters
public org.springframework.batch.core.JobParameters getUniqueJobParameters()- 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 org.springframework.batch.core.JobParametersBuilder getUniqueJobParametersBuilder()- 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.
-
getStepRunner
Convenient method for subclasses to grab aStepRunnerfor running steps by name.- Returns:
- a
StepRunner
-
launchStep
Launch just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.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
-
launchStep
public org.springframework.batch.core.JobExecution launchStep(String stepName, org.springframework.batch.item.ExecutionContext jobExecutionContext) Launch just the specified step in a surrounding single-step job of typeSimpleJobnamedStepRunner.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 launchjobExecutionContext- An ExecutionContext whose values will be loaded into the Job ExecutionContext prior to launching the step.- Returns:
- JobExecution
-
launchStep
public org.springframework.batch.core.JobExecution launchStep(String stepName, org.springframework.batch.core.JobParameters jobParameters) Launch 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:
stepName- The name of the step to launchjobParameters- The JobParameters to use during the launch- Returns:
- JobExecution
-
launchStep
public org.springframework.batch.core.JobExecution launchStep(String stepName, org.springframework.batch.core.JobParameters jobParameters, @Nullable org.springframework.batch.item.ExecutionContext jobExecutionContext) Launch 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:
stepName- The name of the step to launchjobParameters- The JobParameters to use during the launchjobExecutionContext- An ExecutionContext whose values will be loaded into the Job ExecutionContext prior to launching the step.- Returns:
- JobExecution
-