如何使用 JobLauncherTestUtils 加载 Spring 批处理作业进行测试

How to load Spring Batch Job using JobLauncherTestUtils for tests

我想测试一个我曾经作为 SpringBootTest 和 SpringJunit4Runner 加载的作业。当我升级到 JUnit 5 时,jobLauncherTestUtils class 不再加载。该项目是使用 Spring Boot 2.2.0.RELEASE 的 Spring 批处理应用程序。我的主要配置称为 AppConfig,我已将步骤和作业配置为可以在测试中自动装配的 beans class。但是,以前加载的应用程序上下文现在甚至可以加载更长的时间。该错误告诉我作业未添加到 jobLauncherTestUtils。我不明白为什么以前可以加载的作业不能再加载了。如果能帮我解决这个问题,我将不胜感激

src/main/com/indigo/search/config/AppConfig

    @Bean
    public Step orderIntakeStep() {
       return stepBuilderFactory.get("orderIntakeStep")
               .<Order, Order>chunk(30)
               .reader(orderReader())
               .processor(orderProcessor())
               .writer(orderWriter())
               .build();
    }


    @Bean(name = "orderIntakeJob")
    public Job orderIntakeJob() {
    return jobBuilderFactory.get("orderIntakeJob")
            .incrementer(new RunIdIncrementer())
            .flow(orderIntakeStep())
            .end()
            .build();
     }
      ...
  }



   @ExtendWith(SpringExtension.class)
   @SpringBatchTest
   @Transactional(propagation = Propagation.NOT_SUPPORTED)
   class OrderIntakeJobTest {

       @Autowired
       private JobLauncherTestUtils jobLauncherTestUtils;

       @Autowired
       private JobRepositoryTestUtils jobRepositoryTestUtils;

       @Autowired
       private JobLauncher jobLauncher;

       @Autowired
       Job orderIntakeJob;
       ...

      @Before 
      public void initJob(){
         jobLauncherTestUtils.setLauncher(jobLauncher);
         jobLauncherTestUtils.setJobLauncher(jobLauncher);
         jobLauncherTestUtils.setJobRepository(jobRepository);
         jobLauncherTestUtils.setJob(orderIntakeJob);
      j

   }

从您分享的内容来看,没有任何内容可以导入包含 OrderIntakeJobTest 中正在测试的作业的配置 class。你应该有这样的东西:

@ExtendWith(SpringExtension.class)
@SpringBatchTest
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@ContextConfiguration(classes = MyJobConfiguration.class) // this is where the job under test is defined
class OrderIntakeJobTest {

   // ...

}