首页 > 解决方案 > 如何使用 Mockito 编写 Spring Batch 测试用例?

问题描述

我正在寻找为我编写测试用例Spring Batch Job,为此我正在通过链接:https ://docs.spring.io/spring-batch/docs/4.0.x/reference/html/testing.html#creatingUnitTestClass我在测试用例下面开发,我意识到它实际上像真正的批处理作业调用一样工作,这可能是可悲的。

我想简单地模拟所有批处理作业。是否有任何 OOTB API 或者我可以简单地使用 Mockito 来制作它?

@SpringBatchTest
@SpringBootTest
@RunWith(SpringRunner.class)
public class Job {
    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @BeforeClass
    public static void beforeClass() {
        // set some env variables to allow Spring Context to load up !
    }
    
    @Test
    public void test() throws Exception {
        JobExecution jobExecution = this.jobLauncherTestUtils.launchJob();    
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

        StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();

        assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus());
    }
}

标签: spring-batch

解决方案


推荐阅读