spring-batch - Spring Batch 中的步骤测试
问题描述
在使用 jobLauncherTestUtils.launchstep() 测试一个包含读取器和写入器的面向块的处理步骤时。
1.单步测试时如何通过或设置job explorer?
步:
@Bean(name="step_name")
public step step1(RepositoryItemReader<Person> perosnItemReader,
FlatFileItemWriter<String> personItemWriter,
TaskExecutor personTaskExecutor){
return StepBuilderFactory
.get("step_name")
.listener(new listener())
.<Person,String>Chunk(200)
.reader(personItemReader)
.writer(personItemWriter)
.listener(new DateReadListener())
.build();
}
读者:
@Bean(name="step_reader")
public RepositoryItemReader<Person> personItemReader(
@Value("#jobParameters[id]}" String id){
return new RepositoryItemReader<Person>()
.name("step_reader")
.repository(personDao)
.methodName("getDetails")
.arguments(id)
.build();
}
作家:
@Bean(name="step_writer")
public FlatFileItemWriter<String> personItemWriter(
@Value("#{jobExecutionContext[person]}") Person person,
@Value("#{stepExecution.jobExecution.id}" long id,
JobExplorer jobExplorer) {
Foo foo = new
Foo(jobExplorer.getJobExecution(jobId).getExecutionContext(),new
CurrentDate());
return new FlatFileItemWriterBuilder()
.get("item_writer")
.callback(foo)
.build();
}
我测试它的方式。
ExecutionContext executionContext = new ExecutionContext()
executionContext.put("person",Person);
JobExecution jobExecution =
JobLauncherTestUtils.launchStep("step_name",jobparams,executionContext);
问题
class Foo{
Foo f = new Foo(JobExecution jobExection, Date date);
Person person = (Person)jobExecution.getExecutionContext().get("person");
}
在这里,jobExecution 返回空值。
提前感谢
解决方案
我如何检查阅读器是否成功返回数据?
您无需测试整个步骤即可检查阅读器是否正确返回数据。阅读器本身的单元测试就足够了:创建并打开阅读器,调用read
并断言结果。
2.如何将作业执行ID和作业资源管理器传递给以两个值作为参数的写入器?
为什么项目编写者需要作业执行和作业浏览器?从您的代码中,创建编写器时不使用这些。
推荐阅读
- linux - 需要搜索模式并打印下一个值
- javascript - 如果我在 for 循环上方声明“i”,我得到的输出为 3 3 3,如果我在 for 循环中声明“i”,我得到的输出为 1 2 3
- python-3.x - 我希望只有管理员能够使用我的 Discord.py 机器人的这个命令
- c++ - 如何使用C++通过dll调用Delphi类
- c - Vim Airline 将 .h 文件解释为 cpp,而不是 c
- jboss - Wildfly 13 ejb 无状态会话 bean 未从池中删除
- python - 关于 Anaconda 的 python 和原生 macOS python
- dart - 如何在 Dart 中检查和转换泛型参数?
- arm - 理解这行乘以 8 的 VHDL 代码
- asp.net-core - 添加对程序集 .net 核心的引用