【问题标题】:Unit Testing using JUnit for Spring Batch without XML configuration使用 JUnit 对 Spring Batch 进行单元测试,无需 XML 配置
【发布时间】:2017-07-28 12:59:04
【问题描述】:

我是 Spring Batch 的新手,我开始开发一个简单的批处理应用程序。现在我正在考虑一些单元测试 unsing JUnit 可能对我的应用程序和代码有益;)

问题是我在 Internet 上找不到任何资源(示例、教程...)来说明如何在不使用 XML 时使用 Spring Batch 执行单元测试。

这是我的代码更清楚:

配置类:

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 类将通过其 main 方法启动 tasklet:

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 Batch进行单元测试吗?
  • 其实我没有。

标签: spring unit-testing spring-batch junit4 xml-configuration


【解决方案1】:

我使用 Spring Batch 和 MyBatis 和 JUnit 制作了一个简单的批处理应用程序。

应用程序的测试代码在没有 XML 的情况下运行单元测试。

这是 Job 的测试类。


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {xxx.class, yyy.class, zzz.class, xxxJobLauncherTestUtils.class})
public class JobTest {

    @Autowired
    @Qualifier(value = "xxxJobLauncherTestUtils")
    private JobLauncherTestUtils xxxJobLauncherTestUtils;

    @Test
    public void testXxxJob() throws Exception {
        JobExecution jobExecution = xxxJobLauncherTestUtils.launchJob();
        assertThat(jobExecution.getStatus(), is(BatchStatus.COMPLETED));
    }
}

@Component(value = "xxxJobLauncherTestUtils")
class XxxjobLauncherTestUtils extends JobLauncherTestUtils {

    @Autowired
    @Qualifier(value = "xxxJob")
    @Override
    public void setJob(Job job) {
        super.setJob(job);
    }
}

详情请看以下链接。

https://github.com/Maeno/spring-batch-example/tree/master/src/test

希望对你有所帮助。

【讨论】:

  • 提供解决方案或示例代码的链接很好,但如果您在回答中提供更多详细信息来解决 OP 的问题以及链接,那就太好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2015-07-08
相关资源
最近更新 更多