2017-07-28 78 views
0

我是Spring Batch的新手,我開始開發一個簡單的批處理應用程序。現在我正在考慮一些單元測試未知的JUnit,這可能是我的應用程序和代碼健康)使用JUnit進行單元測試,無需XML配置的Spring批處理

問題是,我找不到任何資源(例子,tutos ...)在互聯網上,顯示如何在不使用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類,將它的主要方法啓動任務蕾:

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); 
    } 
} 
+0

您是否瞭解**如何在使用XML **時使用Spring Batch執行單元測試? –

+0

其實不,我不知道。 –

回答

-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

我希望這將是有益的。

+1

提供解決方案或示例代碼的鏈接沒有問題,但如果您在鏈接之外提供了有關OP的問題的更多詳細信息,那將會很好。 – Nima

相關問題