无法使用 groovy/spock 从端到端启动 Spring 批处理作业 - 集成测试

Unable to launch from end to end Spring batch Job using groovy/spock - integration testing

我最近开始进行 groovy 测试,以测试 spring 使用一个步骤的批处理作业(ItemReader、ItemProcessor、ItemWriter)

知道我正在使用 h2 内存数据库,我也尝试通过单元测试启动作业,但我无法验证作业是否已完成:

    def 'test launch job'() {
    given:
    def projectId = 1234L
    def fileType = FileType.TEST
    def sourceId = 1234506L
    def enteredBy = 'groovy_unit_test'
    def prePlanYear = 2022L

    when:
 
    JobParametersBuilder builder = new JobParametersBuilder();
    builder.addLong("projectId", projectId)
            .addString("fileType", fileType.toString())
            .addLong("sourceId", sourceId)
            .addString("enteredBy", enteredBy)
            .addLong("prePlanYear", prePlanYear)
    //JobExecution jobExecution = jobLauncher.launchJob( builder.toJobParameters())
    JobExecution jobExecution = jobLauncher.launchStep(JobConstants.StepNames.xwalkMappingStep.name(), builder.toJobParameters())
  then: 'Job completes with no errors.'
   jobExecution.status == BatchStatus.COMPLETED
   //jobExecution.exitStatus == ExitStatus.COMPLETED
}

作业的状态始终保持已启动:

此外,当我尝试调试时,它并没有从头到尾进行,一旦测试完成,它就会在 ItemReader 中停止。我看到它不等待作业完成。

有没有办法在作业完成后才完成测试?

@PropertySource('classpath:application.properties')
@ActiveProfiles('test')
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = ["spring.h2.console.enabled=true"])
@ImportAutoConfiguration([ FeignAutoConfiguration.class])
@Import([TestConfig.class])
class BaseSpec extends Specification{

    @PersistenceContext
    EntityManager entityManager

    @Autowired
    JobRepository jobRepository

    @Autowired
    DataSource dataSource

    @Autowired
    JpaTransactionManager jpaTransactionManager

    @Autowired
    JobLauncherTestUtils jobLauncher

}

@Configuration
@EnableBatchProcessing
class TestConfig {
}

谢谢

那是因为您没有 运行 整个作业,而只是这里的步骤:

JobExecution jobExecution = jobLauncher.launchStep(JobConstants.StepNames.xwalkMappingStep.name(), builder.toJobParameters())

你应该 运行 工作 JobLauncher#launchJob 而不仅仅是步骤。