Spring 动态创建作业的批量测试
Spring batch test for dynamically created job
在我的应用程序中,我有多个作业所以我创建了动态 jobs.I 在这个应用程序中 运行 没有问题。我想对动态创建的作业进行单元测试。
我想将我的作业设置为 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();
}
}
}
在我的应用程序中,我有多个作业所以我创建了动态 jobs.I 在这个应用程序中 运行 没有问题。我想对动态创建的作业进行单元测试。
我想将我的作业设置为 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();
}
}
}