Spring Batch:测试JobExecutionListener

时间:2014-02-28 13:33:40

标签: testing listener spring-batch jobs

我得到了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 个答案:

答案 0 :(得分: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创建作业上下文
e.g。

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