单元测试中使用spring注入

参考:http://lijingshou.iteye.com/blog/2269593



import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bluedon.useradmin.datainteraction.commontype.OperationStatus;
import com.bluedon.useradmin.datainteraction.commontype.TableType;
import com.bluedon.useradmin.datainteraction.core.VolunteerDataInteractionServiceImpl;
import com.bluedon.useradmin.datainteraction.model.UploadDataRecord;
import com.bluedon.useradmin.datainteraction.model.bd.metadata.BDVolunteer;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/application-context-test.xml"})
public class BDVolunteerTest {
	
	@Resource
	private VolunteerDataInteractionServiceImpl volunteerDataInteractionService;
	
	@Test
	public void testAdd(){
		BDVolunteer volunteer = new BDVolunteer();
		volunteer.setZyzbh("88888");
		volunteer.setZjlx("440784");
		volunteer.setZjhm("4407845");
		volunteer.setXm("李四");
		volunteer.setCsrq(new Date());
		volunteer.setXingbie("1");
		volunteer.setGuoji("中国");
		volunteer.setMingzu("汉族");
		volunteer.setZzmm("群众");
		volunteer.setXueli("本科");
		volunteer.setJgsf("110000");
		volunteer.setJgs("110000");
		volunteer.setJzsf("110000");
		volunteer.setJzs("110000");
		volunteer.setJzxq("110000");
		volunteer.setYddh("138244");
		volunteer.setCyzt("失业");
		volunteer.setLjfwsj(100L);
		volunteer.setLjpxsj(50L);
		volunteer.setZcsj(new Date());
		
		UploadDataRecord obj = new UploadDataRecord();
		obj.setSystemCode("izy");
		obj.setBatch("123");
		obj.setCreateTime(new Date());
		obj.setFlag("aaa");
		obj.setOperation(OperationStatus.ADD.getValue());
		obj.setTable(TableType.BDVolunteer.getName());
		obj.setData(volunteer);
		
		List<UploadDataRecord> list = new ArrayList<UploadDataRecord>();
		list.add(obj);		
		volunteerDataInteractionService.addUploadData("izy", "123", list);
	}
}

1、maven依赖

<!-- junit -->
	<dependency>
    	<groupId>junit</groupId>
    	<artifactId>junit</artifactId>
    	<version>4.12</version>
    	<scope>test</scope>
	</dependency>
	<!-- spring-test -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-test</artifactId>
	    <version>4.3.9.RELEASE</version>
	    <scope>test</scope>
	</dependency>

2、类名添加注解

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/application-context-test.xml"})

然后即可用@Resource进行注入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值