Spring批处理:单元测试后期绑定
Spring Batch: unit test late binding
我reader配置如下:
<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 已正确注入我的 reader,但作业参数却没有。根据文档,这就是所有需要完成的事情,我发现的唯一问题是关于使用 jobParameters[[=22=]] 而不是 jobParameters[from]。知道哪里出了问题吗?
此外,在我的 reader 进入测试方法之前没有调用 open(executionContext) 方法,这是不行的,因为我使用那些作业参数来检索一些需要可用的数据当调用 read 方法时。这可能与上述问题有关,因为有关后期绑定测试的文档说 "The reader is initialized and bound to the input data".
您正在将 from
和 to
设置为测试中的步骤执行上下文变量。但是在您的应用程序上下文配置中,您将它们作为作业参数进行检索。您应该在单元测试中将它们设置为作业参数。
此外,如果您希望调用 open/update/close ItemStream
生命周期方法,则应执行该步骤。参见 http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/test/JobLauncherTestUtils.html#launchStep-java.lang.String-org.springframework.batch.core.JobParameters-
我reader配置如下:
<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 已正确注入我的 reader,但作业参数却没有。根据文档,这就是所有需要完成的事情,我发现的唯一问题是关于使用 jobParameters[[=22=]] 而不是 jobParameters[from]。知道哪里出了问题吗?
此外,在我的 reader 进入测试方法之前没有调用 open(executionContext) 方法,这是不行的,因为我使用那些作业参数来检索一些需要可用的数据当调用 read 方法时。这可能与上述问题有关,因为有关后期绑定测试的文档说 "The reader is initialized and bound to the input data".
您正在将 from
和 to
设置为测试中的步骤执行上下文变量。但是在您的应用程序上下文配置中,您将它们作为作业参数进行检索。您应该在单元测试中将它们设置为作业参数。
此外,如果您希望调用 open/update/close ItemStream
生命周期方法,则应执行该步骤。参见 http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/test/JobLauncherTestUtils.html#launchStep-java.lang.String-org.springframework.batch.core.JobParameters-