gpt4 book ai didi

unit-testing - 使用参数测试单个 Spring Batch Tasklet 步骤

转载 作者:行者123 更新时间:2023-12-04 04:48:17 32 4
gpt4 key购买 nike

我已经用 2 个小任务实现了一个简单的工作。我想通过传递参数来测试第二个 tasklet。

我已经阅读了 Spring 批处理文档和下面的测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({"test"})
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
public class EtudeBatchApplicationTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

public StepExecution getStepExecution() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getJobExecution().getExecutionContext().putString("myValue", "foo,bar,spam");
return execution;
}

@Test
public void contextLoads() {

JobExecution jobExecution = jobLauncherTestUtils.launchStep("insertIncludedSiretsStep");
}

}

我的问题是在我的 tasklet 中,myValue 始终为空。

下面是tasklet的代码:
@Component
@StepScope
@Slf4j
public class InsertIncludedSiretsTask implements Tasklet {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
Object myValue = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("myValue");

log.info("INSERTINCLUDEDSIRETSTASK runnning");
Thread.sleep(3000);
return RepeatStatus.FINISHED;

}
}

最佳答案

您可以按照以下示例模拟 ChunkContext:

http://www.javased.com/index.php?source_dir=spring-insight-plugins/collection-plugins/spring-batch/src/test/java/com/springsource/insight/plugin/springbatch/TaskletCollectionAspectTest.java

这是我的代码:

public ChunkContext createChunkContext() {
StepExecution stepExecution=Mockito.mock(StepExecution.class);
StepContext stepContext=Mockito.mock(StepContext.class);
ChunkContext chunkContext=Mockito.mock(ChunkContext.class);
JobExecution jobExecution= createJobExecution();

Mockito.when(chunkContext.getStepContext()).thenReturn(stepContext);
Mockito.when(stepContext.getStepExecution()).thenReturn(stepExecution);
Mockito.when(stepExecution.getJobExecution()).thenReturn(jobExecution);

return chunkContext;
}
public JobExecution createJobExecution() {
JobExecution execution = MetaDataInstanceFactory.createJobExecution();
execution.getExecutionContext().putString("myValue", "foo,bar,spam");
return execution;
}

@Test
public void testSendEmail() throws Exception {
StepContribution contribution= Mockito.mock(StepContribution.class);
ChunkContext chunkContext= createChunkContext();


sendReportTasklet.execute(contribution, chunkContext );
}

关于unit-testing - 使用参数测试单个 Spring Batch Tasklet 步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44452518/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com