【发布时间】:2015-01-21 14:29:58
【问题描述】:
我的阅读器配置如下:
<bean name="reader" class="...Reader" scope="step">
<property name="from" value="#{jobParameters[from]}" />
<property name="to" value="#{jobParameters[to]}" />
<property name="pageSize" value="5"/>
<property name="saveState" value="false" /> <!-- we use a database flag to indicate processed records -->
</bean>
并像这样对其进行测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:testApplicationContext.xml"})
@ActiveProfiles({"default","mock"})
@TestExecutionListeners( {StepScopeTestExecutionListener.class })
public class TestLeadsReader extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private ItemStreamReader<Object[]> reader;
public StepExecution getStepExecution() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getExecutionContext().putLong("campaignId", 1);
execution.getExecutionContext().putLong("partnerId", 1);
Calendar.getInstance().set(2015, 01, 20, 17, 12, 00);
execution.getExecutionContext().put("from", Calendar.getInstance().getTime());
Calendar.getInstance().set(2015, 01, 21, 17, 12, 00);
execution.getExecutionContext().put("to", Calendar.getInstance().getTime());
return execution;
}
@Test
public void testMapper() throws Exception {
for (int i = 0; i < 10; i++) {
assertNotNull(reader.read());
}
assertNull(reader.read());
}
现在,虽然 pageSize 和 saveState 已正确注入我的阅读器,但作业参数却没有。根据文档,这就是它需要做的所有事情,我发现的唯一问题是关于使用 jobParameters['from'] 而不是 jobParameters[from]。知道有什么问题吗?
另外,我的reader在进入测试方法之前没有调用open(executionContext)方法,这是不行的,因为我使用那些作业参数来检索一些在调用read方法时需要可用的数据.不过,这可能与上述问题有关,因为有关使用后期绑定进行测试的文档说“读取器已初始化并绑定到输入数据”。
【问题讨论】:
标签: testing spring-batch late-binding