我正在尝试测试Spring Batch步骤。我有2个场景要测试 1.使用tasklet步骤(步骤范围) 2.使用ItemReader / ItemWriter(步骤范围)步骤
我的测试类注释如下
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class})
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = ItemReturnsApplication.class)
This is how my test class looks like
@Bean
JobLauncherTestUtils jobLauncherTestUtils(){
return new JobLauncherTestUtils();
}
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testLaunchJob() throws Exception{
JobExecution jobExecution = jobLauncherTestUtils.launchJob(
new JobParametersBuilder().addString("chunkSize", "3").addString("current_date","2016-11-25").toJobParameters()
);
commonAssertions(jobLauncherTestUtils.launchJob());
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
当我运行测试用例时,该过程失败,因为作业参数没有传递到我的工作中。
我正在寻找在春季批次中测试Step Scoped步骤的正确方法。
谢谢, Opensource Explorer
答案 0 :(得分:0)
您当前的代码尝试启动并测试作业而不是步骤。根据关于如何测试各个步骤的spring批处理documentation,如何测试tasklet并将上下文注入到tasklet中的一个简单示例更符合以下代码:
TARGET = raspberrypi_qt
@ContextConfiguration
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class StepScopeTestExecutionListenerIntegrationTests {
// This component is defined step-scoped, so it cannot be injected unless
// a step is active...
@Autowired
private YourTaskletClass yourTaskletClass;
public StepExecution getStepExection() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getExecutionContext().putString("input.data", "foo,bar,spam");
return execution;
}
@Test
public void testYourTaskletClass() {
// The tasklet is initialized and some configuration is already set by the MetaDatAInstanceFactory
assertNotNull(yourTaskletClass.doSomething());
}
}
注释只能在1.4及更高版本的弹簧启动版本中使用。有关详细信息,请参阅this博客。
要启动单个步骤,请尝试将代码调整为:
@RunWith(SpringJUnit4ClassRunner.class)