Skip to content

SpringBatchTest does not work ootb with SpringBootΒ #4233

Closed

Description

@eiswind

Given the following job definition with Spring Boot 3-RC2:

@Configuration
public class HelloBatch {

	public static void main(String[] args) {
		SpringApplication.run(BatchApplication.class, args);
	}
	@Bean
	public Step step(JobRepository jobRepository, PlatformTransactionManager tx) {
		return new StepBuilder( "step1", jobRepository)
				.tasklet((contribution, chunkContext) -> {
					System.out.println("Hello, World!");
					return RepeatStatus.FINISHED;
				}, tx).build();
	}

	@Bean
	public Job job(JobRepository jobRepository, Step step) {
		return new JobBuilder("job", jobRepository)
				.start(step)
				.build();
	}
}

The following test (as suggested in the javadoc) does fail:

@SpringBatchTest
@SpringJUnitConfig(HelloBatch.class)
public class HelloBatchTest {

	@Autowired
	private JobLauncherTestUtils jobLauncherTestUtils;

	@Test
	public void testHello(@Autowired Job job) throws Exception {

		jobLauncherTestUtils.setJob(job);

		var jobExecution =
				jobLauncherTestUtils.launchJob();

		assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

	}
}

the exception goes here

Error creating bean with name 'jobLauncherTestUtils': Unsatisfied dependency expressed through method 'setJobRepository' parameter 0:

I could workaround by making the test a SpringBootTest and disabling the auto-configured joblauncher but i guess that is not the intented solution.

Activity

  1. added this to the 5.0.0 milestone on Nov 15, 2022
  2. fmbenhassine commented on Nov 16, 2022

    @fmbenhassine
    Contributor

    Thank you for opening this ticket. This is a valid issue, and is related to the new way test utilities are configured with @SpringBatchTest in v5. Test utilities should not autowire dependencies. I noticed that this is inconsistent as of 5.0.0-RC2 (the job under test is not autowired while the job repository and job launcher are). This has been fixed in 05bbd3f.

    Now on the Boot side, things are a bit different with regard to the setup of the test context. I noticed that without importing the HelloBatch.class and using @SpringBootTest the test you shared passes. This is actually how I am expecting things to work in a Boot world: the simple fact of using @SpringBootTest should import my job configuration (without having to do it manually). This is not a Javadoc issue because the sample in the Javadoc of Spring Batch works with Spring Batch alone.

    Could you please give the latest snapshots a try? I am expecting the following test to pass with the latest snapshots:

    import org.junit.jupiter.api.Test;
    
    import org.springframework.batch.core.BatchStatus;
    import org.springframework.batch.test.JobLauncherTestUtils;
    import org.springframework.batch.test.context.SpringBatchTest;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    @SpringBatchTest
    @SpringBootTest
    public class HelloBatchTest {
        
        @Autowired
        private JobLauncherTestUtils jobLauncherTestUtils;
        
        @Test
        public void testHello() throws Exception {
            var jobExecution =
                    jobLauncherTestUtils.launchJob();
    
            assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        }
    
    }
  3. eiswind commented on Nov 16, 2022

    @eiswind
    Author

    I can confirm that the test passes, but it runs the job twice. (The JobLauncher from the AutoConfiguration...)

    I added

    @SpringBootTest
    @SpringBatchTest
    @TestPropertySource(properties = "spring.batch.job.enabled=false")
    public class HelloBatchTest {

    then everything is fine.

    Maybe this should be the default in a test?

  4. fmbenhassine commented on Nov 16, 2022

    @fmbenhassine
    Contributor

    Thank you for taking the time to test the fix! Much appreciated πŸ‘

    Maybe this should be the default in a test?

    This is not specific to Spring Batch per se. Please check/request this with the Boot team. Thank you upfront.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions