Reputation: 9261
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.
Upvotes: 4
Views: 1763
Reputation: 4444
You can't do that. I asked the same question a couple of weeks ago: Using @SpringApplicationConfiguration: How to set jobparameters in tests when using spring-batch and spring-boot
You have to directly use the JobLauncherCommandLineRunner:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyBatchRootConfiguration.class})
@IntegrationTest({"aProp=aValue"})
public class MyBatchTestClass {
@Autowired
private JobLauncherCommandLineRunner runner;
@Test
public void launchTest() {
String[] myArgs = new String[]{"jobParam1=value1"};
runner.run(myArgs);
}
}
Upvotes: 3