私はSpring Batchを初めて使用しました。私は簡単なバッチアプリケーションを開発し始めました。今では、私のアプリとコードで健全なJUnitのユニットテストについて考えています;)JUnitを使ったSpringバッチでのXML設定なしのユニットテスト
問題は、インターネット上でどのようにリソースを見つけることができなかったかです。 XMLを使用しない場合、Spring Batchを使用してユニットテストを実行します。ここで
はより明確にすることが私のコードです:
Configクラス:
package my.company.project.name.batch.config
@Configuration
@EnableBatchProcessing
@ComponentScan({
"my.company.project.name.batch.reader",
"my.company.project.name.batch.tasklet",
"my.company.project.name.batch.processor",
"my.company.project.name.batch.writer"
})
@Import({CommonConfig.class})
public class MyItemBatchConfig {
@Autowired
private StepBuilderFactory steps;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private MyItemTasklet myItemTasklet;
@Bean
public Job myItemJob(@Qualifier("myItem") Step loadProducts){
return jobBuilderFactory.get("myItemJob").start(myMethod).build();
}
@Bean(name= "myItem")
public Step myMethod(){
return steps.get("myItem").tasklet(myItemTasklet).build();
}
}
MyItemReaderクラス:
package my.company.project.name.batch.reader
@Component
public class MyItemReader implements ItemReader<MyItem>{
@Value("${batch.load.produit.csv.file.path}")
private String csvFilePath;
private LinkedList<CsvRawLine> myItems;
@PostConstruct
public void init() {
myItems = new LinkedList<>(CsvUtil.getCsvReader(MyItem.class, csvFilePath));
}
@Override
public MyItem read() throws Exception{
return myItems.poll();
}
}
ItemProcessorクラス:
package my.company.project.name.batch.processor
@Component
public class MyItemProcessor implements ItemProcessor<MyItem, MyItemProcessorResult> {
public MyItemProcessorResult process(MyItemitem) throws Exception {
//processing business logic
}
}
ItemWriterクラス:タスクはバッチによってたかっ達成するために、以前のすべてのクラスを呼び出します
package my.company.project.name.batch.writer
@Component
public class MyItemWriter implements ItemWriter<MyItem> {
@Override
public void write(List<? extends MyItem> myItems) throws Exception {
//writer business logic
}
}
MyItemTaskletクラス:その主な方法でタスクレットを起動します
package package my.company.project.name.batch.tasklet
@Component
public class MyItemBatchTasklet implements Tasklet{
@Autowired
public MyItemReader myItemReader;
@Autowired
public MyItemProcessor myItemProcessor;
@Autowired
public MyItemeWriter myItemWriter;
@Override
public RepeatStatus execute execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
//calling myItemReader, myItemProcessor and myItemWriter to do the business logic
return RepeatStatus.FINISHED
}
}
MyItemTaskletクラス:
package package my.company.project.name.batch
public class MyItemTaskletLauncher{
public MyItemTaskletLauncher(){
//No implementation
}
public static void main (String[] args) throws IOException, JobExecutionException, NamingException {
Launcher.launchWithConfig("Launching MyItemTasklet ...", MyItemBatchConfig.class,false);
}
}
XML **を使用している場合、Springバッチで単体テストを実行する方法について知っていますか? –
実際はそうではありません。 –