繁体   English   中英

Spring 动态创建作业的批量测试

[英]Spring batch test for dynamically created job

在我的应用程序中,我有多个作业所以我创建了动态作业。我在运行这个应用程序时没有问题。 我想对动态创建的作业进行单元测试。

我想将我的工作设置为 JobLauncherTestUtils。

@RunWith(SpringRunner.class)
@SpringBatchTest()
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@PropertySource("classpath:application.yml")
public class SpringBatchIntegrationTest {
    @Inject
    private JobRepository jobRepository;
    @Inject
    private JobLauncher mJobLauncher;
    private JobLauncherTestUtils jobLauncherTestUtils;
    @Inject
    BatchJobConfig mBatchJobConfig;
    public void initailizeJobLauncherTestUtils() {
        jobLauncherTestUtils = new JobLauncherTestUtils();
        jobLauncherTestUtils.setJobRepository(jobRepository);
        jobLauncherTestUtils.setJob(mBatchJobConfig.createJob());
        jobLauncherTestUtils.setJobLauncher(mJobLauncher);
    }

这就是我初始化 JobLauncherTestUtils 的方式。 当我运行这个时,我得到以下错误错误创建名为'jobLauncherTestUtils'的bean:通过方法'setJob'参数0表示的不满足的依赖关系; 谁能告诉我如何对动态作业进行 spring 批量测试。 我对 Junit 了解不多。 我刚开始学习

@SpringBatchTest已经在您的测试上下文中添加了一个JobLauncherTestUtils类型的 bean(请参阅Javadoc ),因此您不需要自己添加它。

但是, JobLauncherTestUtils需要一个作业 bean,而且看起来您没有在测试上下文中定义一个作业 bean。 您可以做的是在配置 class 中定义一个并将其导入您的测试上下文中,例如:

@RunWith(SpringRunner.class)
@SpringBatchTest
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
@PropertySource("classpath:application.yml")
@ContextConfiguration
public class SpringBatchIntegrationTest {
   @Inject
   private JobRepository jobRepository;
   @Inject
   private JobLauncher mJobLauncher;
   @Inject
   private JobLauncherTestUtils jobLauncherTestUtils;

   // No need for initailizeJobLauncherTestUtils

   // Add your test method

   @Configuration
   @Import(BatchJobConfig.class) // you might need this or not depending on what's defined in BatchJobConfig
   static class MyJobConfiguration {

      @Bean
      public Job job(BatchJobConfig mBatchJobConfig) {
         return mBatchJobConfig.createJob();
      }

   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM