SpringBatch:测试JobExecutionListener [英] SpringBatch: Test a JobExecutionListener

查看:752
本文介绍了SpringBatch:测试JobExecutionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了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.您认为可以吗,还是对此有另一个不错的解决方案?

I want to write a test for my JobListener and i am wondering myself if i need to mock the JobExecution. Do you think that will be ok, or is there another nice solution to this?

推荐答案

Spring批处理附带了一个不错的测试框架.
将以下内容添加到您的pom.xml

Spring batch comes with a nice testing framework.
Add the following to your 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创建作业上下文
例如

If you want to unit test this class, you may use the org.springframework.batch.test.MetaDataInstanceFactory to create a job context
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

If you want to make this into an integration test write a snippet job xml that contains some mocked job and use the org.springframework.batch.test.JobLauncherTestUtils
For more information see http://docs.spring.io/spring-batch/reference/html/testing.html

这篇关于SpringBatch:测试JobExecutionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆