I have defined multi Spring Batch Jobs in a Spring Boot application. For example, job1, job2. etc.
When I have written a junit test to one of these jobs. The problem is when I reviewed the test output log, I found it tried to launch all jobs defined in this project.
I am using the latest stable Spring Boot 1.2.5, Spring Batch 3.0.4 in the projects.
The fragment code of junit test is following.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@TransactionConfiguration
@SpringApplicationConfiguration(classes = BatchApplication.class)
public class SubmitJobTest {
@Inject Job job1;
@Test
public void testLockJob() {
logger.debug("lockId is @" + task.getLockId());
JobParametersBuilder builder = new JobParametersBuilder()
.addString("lockId", lockId.toString());
try {
JobExecution jobExecution = jobLauncher.run(this.job1, builder.toJobParameters());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException ex) {
ex.printStackTrace();
}
}
Some jobs I defined needs a JobParamters to run, so when I ran this test, other Jobs lauched and executed, then threw exceptions due to lack of the specific JobParamters.
I have tried to add @Named
to Job and inject it by an unique name, but got the same result.
I resolved this issue myself. After added spring.batch.job.enabled=false
into application.yml
, it works.
- The jobs are not launched by default.
- When ran the tests, it worked as I expected, only one job launched.