Skip to content

JobOperatorTestUtils only allows one Job per Spring context #5118

@prismspike

Description

@prismspike

Bug description
After upgrading to Spring Boot 4 and Spring Batch 6 multiple tests are failing in our project, using JobOperatorTestUtils. After wiring multiple Jobs and using JobOperatorTestUtils to start them, only the first job is successfully executed, while every job after that is failing with a JobInstanceAlreadyCompleteException. The logged JobParameters are always the params of the first job run, even when attaching another Job to JobOperatorTestUtils.

Message: A job instance already exists and is complete for identifying parameters={JobParameter{name='batch.random', value=4546055881725385948, type=class java.lang.Long, identifying=true}}

Environment

  • Spring Boot 4.0.0
  • spring-boot-starter-batch 6.0.0
  • spring-batch-test 6.0.0
  • MariaDB 11.4

Steps to reproduce

  • Add two Job Beans via JobBuilder with different names and qualifiers
  • Wire both jobs in your Spring JUnit Test
  • Wire JobOperatorTestUtils
  • Add two tests and set each job on the utils, once for each test
  • Start the job

Expected behavior
The setting of the job on JobOperatorTestUtils should override the previous config and be able to run again

Minimal Complete Reproducible example

@SpringBatchTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class BatchTest {
    @Autowired
    lateinit var jobOperatorTestUtils: JobOperatorTestUtils

    @Autowired
    @Qualifier("jobA")
    private lateinit var jobA: Job

    @Autowired
    @Qualifier("jobB")
    private lateinit var jobB: Job

    @Test
    @Throws(Exception::class)
    fun testJob() {
        jobOperatorTestUtils.setJob(jobA)
        val params = JobParametersBuilder()
            .addLocalDate("runDate", LocalDate.now())
            .addString("CID", UUID.randomUUID().toString())
            .toJobParameters()

        jobOperatorTestUtils.startJob(params)
    }

    @Test
    @Throws(Exception::class)
    fun testJob2() {
        jobOperatorTestUtils.setJob(jobB)
        val params = JobParametersBuilder()
            .addLocalDate("runDate", LocalDate.now())
            .addString("CID", UUID.randomUUID().toString())
            .toJobParameters()

        jobOperatorTestUtils.startJob(params)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions