SpringBatch: Test a JobExecutionListener

邮差的信 提交于 2019-12-13 04:42:12

问题


I got a JobExecutionListener as follows:

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
    }
}

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?


回答1:


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>

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);

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



来源:https://stackoverflow.com/questions/22096617/springbatch-test-a-jobexecutionlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!