Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringBatchTest does not work ootb with SpringBoot #4233

Closed
eiswind opened this issue Nov 15, 2022 · 3 comments
Closed

SpringBatchTest does not work ootb with SpringBoot #4233

eiswind opened this issue Nov 15, 2022 · 3 comments

Comments

@eiswind
Copy link

eiswind commented Nov 15, 2022

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.

@eiswind eiswind added status: waiting-for-triage Issues that we did not analyse yet type: bug labels Nov 15, 2022
@fmbenhassine fmbenhassine added in: test and removed status: waiting-for-triage Issues that we did not analyse yet labels Nov 15, 2022
@fmbenhassine fmbenhassine added this to the 5.0.0 milestone Nov 15, 2022
@fmbenhassine
Copy link
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());
    }

}

@fmbenhassine fmbenhassine added the status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter label Nov 16, 2022
@eiswind
Copy link
Author

eiswind commented Nov 16, 2022

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?

@fmbenhassine
Copy link
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.

@fmbenhassine fmbenhassine removed the status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter label Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants