Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何在Spring批处理的单步单元测试中模拟定制JdbcCursorItemReader?_Spring Boot_Spring Batch - Fatal编程技术网

Spring boot 如何在Spring批处理的单步单元测试中模拟定制JdbcCursorItemReader?

Spring boot 如何在Spring批处理的单步单元测试中模拟定制JdbcCursorItemReader?,spring-boot,spring-batch,Spring Boot,Spring Batch,我正在尝试创建一个Junit测试用例,用于测试批处理作业的单个步骤,而批处理作业又有一个自定义的JdbcCursorItemReader,并将数据写入平面文件。我无法模拟可以注入处理器以写入平面文件的数据。请帮忙 我按照下面链接中的步骤创建了一个测试用例来启动单个步骤: 由于我的reader组件是stepscope,所以我使用上面链接中第10.4节的上下文将所需的域对象放入ExecutionContext中,但在启动步骤时,它不会识别相同的内容,而是执行我通过配置xml注入的实际SQL查询 我

我正在尝试创建一个Junit测试用例,用于测试批处理作业的单个步骤,而批处理作业又有一个自定义的
JdbcCursorItemReader
,并将数据写入平面文件。我无法模拟可以注入处理器以写入平面文件的数据。请帮忙

我按照下面链接中的步骤创建了一个测试用例来启动单个步骤:

由于我的reader组件是stepscope,所以我使用上面链接中第10.4节的上下文将所需的域对象放入
ExecutionContext
中,但在启动步骤时,它不会识别相同的内容,而是执行我通过配置xml注入的实际SQL查询

我的配置xml包含以下步骤:

<step id="sample-step" next="next-step">
 <tasklet>
    <chunk reader="customJDBCReader" processor="customProcessor" 
     writer="customFlatFileItemWriter" commit-interval="XXX" />
 </tasklet>
</step>

预期结果应该是,launchstep需要从
executionContext
而不是从数据库获取数据,才能将数据写入faltfile。

您的预期是错误的。如果该步骤使用了
jdbccbatchitemwriter
,那么该读取器在测试期间仍将发出一个SQL查询,无论您是否在执行上下文中模拟数据。步骤的输入数据与存储在执行上下文中的数据不同

在这种情况下,我建议在测试中使用嵌入式数据库,并用一些虚拟数据填充它


希望这能有所帮助。

您说过您的读者已进入步骤范围。它从执行上下文中消耗了什么?您是否正在使用StepScopeTestExecutionListener?请分享您的读者代码和测试类。@MahmoudBenHassine感谢您的回复,我的读者从配置中执行动态SQL,返回n条记录w.r.t MyDomainObject。我有StepScopeTestExecutionListener,但是我的jobLauncherTestUtils总是执行真正的查询,为我的步骤提供结果。我无法模拟或覆盖自定义阅读器的结果。感谢您的更新!是的,我最近也意识到了这一点,我将尝试使用嵌入式数据库模拟我的数据来解决这个问题。太好了,很高兴它有所帮助。这样的话,谢谢。
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));
}