Spring Batch单元测试示例

本文档详细介绍了如何使用jUnit和TestNG对Spring批处理作业进行单元测试。内容包括设置测试依赖项,创建简单的批处理工作,提供jUnit和TestNG的示例,以及如何声明和执行作业状态。

在本教程中,我们将向您展示如何使用jUnit和TestNG框架对Spring批处理作业进行单元测试。 要对批处理作业进行单元测试,请声明spring-batch-test.jar@ JobLauncherTestUtils ,启动作业或步骤,并声明执行状态。

1.单元测试依赖项

要对Spring批处理进行单元测试,请声明以下依赖项:

pom.xml
<!-- Spring Batch dependencies -->
	<dependency>
		<groupId>org.springframework.batch</groupId>
		<artifactId>spring-batch-core</artifactId>
		<version>2.2.0.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.batch</groupId>
		<artifactId>spring-batch-infrastructure</artifactId>
		<version>2.2.0.RELEASE</version>
	</dependency>
	
	<!-- Spring Batch unit test -->	
	<dependency>
		<groupId>org.springframework.batch</groupId>
		<artifactId>spring-batch-test</artifactId>
		<version>2.2.0.RELEASE</version>
	</dependency>
		
	<!-- Junit -->
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>test</scope>
	</dependency>

	<!-- Testng -->
	<dependency>
		<groupId>org.testng</groupId>
		<artifactId>testng</artifactId>
		<version>6.8.5</version>
		<scope>test</scope>
	</dependency>

2.Spring批作业

一个简单的工作,稍后的单元测试执行状态。

spring-batch-job.xml
<!-- ...... -->
    <batch:job id="testJob">
	<batch:step id="step1">
	    <batch:tasklet>
		<batch:chunk reader="xmlItemReader" 
			writer="oracleItemWriter"
			commit-interval="1">
		</batch:chunk>
	    </batch:tasklet>
	</batch:step>
    </batch:job>

3. jUnit示例

启动作业并声明执行状态。

AppTest.java
package com.mkyong;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/spring-batch-job.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;
    
    @Test
    public void launchJob() throws Exception {

	//testing a job
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        
	//Testing a individual step
        //JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
        
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
        
    }
}

PS假定context.xml被声明为所有必需的Spring批处理核心组件,例如jobRepository等。

必须手动声明此JobLauncherTestUtils

test-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
 
    <!-- Spring should auto load this bean -->
    <bean class="org.springframework.batch.test.JobLauncherTestUtils"/>
    
</beans>

4. TestNG示例

TestNG框架中的等效示例。

AppTest2.java
package com.mkyong;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/spring-batch-job.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest2 extends AbstractTestNGSpringContextTests {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    
    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        Assert.assertEquals(jobExecution.getStatus(), BatchStatus.COMPLETED);
        
    }
}

做完了

下载源代码

下载它– SpringBatch-UnitTest-Example.zip (83 KB)

参考文献

  1. JobLauncherTestUtils JavaDoc
  2. Spring Batch单元测试官方文档

翻译自: https://mkyong.com/spring-batch/spring-batch-unit-test-example/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值