Running Spring Batch Download Issues in Spring Boot Applications

I defined several Spring batch jobs in the Spring boot application. For example, job1, job2. and etc.

When I wrote a junit test for one of these tasks. The problem is that when I looked at the test output log, I found that he tried to run all the tasks defined in this project.

I use the latest versions of Spring Boot 1.2.5, Spring Batch 3.0.4 in projects.

The following is a snippet of the junit test code.

@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(); } } 

For some of the tasks that I defined, I need to run JobParamters, so when I ran this test, other Jobs were started and executed, and then were excluded due to the lack of specific JobParamters.

I tried adding @Named to Job and entering it by a unique name, but got the same result.

I myself have solved this problem. After adding spring.batch.job.enabled=false in application.yml it works.

  • Jobs do not start by default.
  • When running the tests, it worked as I expected, only one task was started.
+4
source share
1 answer

You cannot do this. I asked the same question a couple of weeks ago: Using @SpringApplicationConfiguration: setting job parameters in tests when using spring -batch and spring -boot

You must use JobLauncherCommandLineRunner directly:

 @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); } } 
+2
source

All Articles