2014-02-28 44 views
0

我得到了JobExecutionListener如下:SpringBatch:測試一個JobExecutionListener

public class JobListener implements JobExecutionListener { 

    @Override 
    public void beforeJob(final JobExecution jobExecution) { 
     // do some work with the jobExecution 
    } 

    @Override 
    public void afterJob(final JobExecution jobExecution) { 
     // do some work with the jobExecution 
    } 
} 

我想編寫一個測試我JobListener我想知道自己,如果我需要模擬JobExecution。你認爲那樣會好嗎,還是有另一個很好的解決方案呢?

回答

1

Spring批處理附帶了一個很好的測試框架。
如果你想添加以下到您的pom.xml

<dependency> 
<groupId>org.springframework.batch</groupId> 
    <artifactId>spring-batch-test</artifactId> 
    <version>${spring.batch.version}</version> 
    <scope>test</scope> 
</dependency> 

單元測試這個類,你可以使用org.springframework.batch.test.MetaDataInstanceFactory創造就業方面
例如

JobListener jobListener = new JobListener() 
//Create a JobExecution (You have 6 overloaded methods choose the one you like) 
JobExecution jobExecution = MetaDataInstanceFactory.createJobExecution(..); 
jobListener.beforeJob(jobExecution); 
jobListener.afterJob(jobExecution); 

如果你想使這個成爲一個集成測試編寫包含一些輕慢工作的一個片段作業XML和使用org.springframework.batch.test.JobLauncherTestUtils
欲瞭解更多信息,請參閱http://docs.spring.io/spring-batch/reference/html/testing.html