我正在尝试创建一个Junit测试用例来测试我的批处理作业的单个步骤,而这个步骤又有一个自定义的JdbcCursorItemReader
并将数据写入Flat文件。我无法模拟可以注入处理器的数据,以便将其写入Flatfile。请帮忙。
我按照下面的步骤链接创建了一个测试用例来启动一个单独的步骤:
https://docs.spring.io/spring-batch/trunk/reference/html/testing.html
由于我的阅读器组件是stepcope,我使用上面链接中10.4节的上下文将所需的域对象放入ExecutionContext
但是在启动步骤时它不识别相同而是执行我通过config xml注入的实际SQL查询。
我的配置xml步骤如下:
<step id="sample-step" next="next-step">
<tasklet>
<chunk reader="customJDBCReader" processor="customProcessor"
writer="customFlatFileItemWriter" commit-interval="XXX" />
</tasklet>
</step>
我的自定义读取器实现了JdbcCursorItemReader
,它具有为从数据库中读取信息而定义的SQL。
在我的单元测试期间,我试图模拟数据而不是依赖于数据库中的实际数据
private StepExecution execution;
@BeforeStep
public StepExecution getStepExection() {
execution = MetaDataInstanceFactory.createStepExecution();
return execution;
}
@Test
public void testSampleStep() throws UnexpectedInputException, ParseException, Exception {
MyDomainObject domainObject= new MyDomainObject ();
domainObject.setID("sampleID");
domainObject.setName("sampleName");
domainObject.setImage("sampleImage");
execution.getExecutionContext().put("someKey", domainObject);
execution.setReadCount(1);
JobExecution jobExecution = jobLauncherTestUtils.launchStep("sample-step", jobParameters, execution.getExecutionContext());
AssertFile.assertFileEquals(new FileSystemResource(EXPECTED_FILE), new FileSystemResource(OUTPUT_FILE));
}
预期的结果应该是launchstep需要从executionContext
而不是数据库获取数据以将其写入faltfile。
你的期望是错误的。如果该步骤使用JdbcBatchItemWriter
,则无论您是否在执行上下文中模拟数据,此读取器仍将在测试期间发出SQL查询。步骤的输入数据与存储在执行上下文中的数据不同。
在这种情况下,我建议在测试中使用嵌入式数据库,并使用一些虚拟数据填充它。
希望这可以帮助。