介绍

FDP快速开发平台,为做JUnit单元测试做了些准备工作。

FDP使用Spring框架,Service层、Dao层的Bean对象都由Spring容器管理。

所以当需要对Service层、Dao层做JUnit单元测试时,要先实例化Spring 容器,准备好Spring上下文环境,才能运行Service层、Dao层的Bean对象

FDP框架提供了 SpringTransactionalContextTests 基类,可自动加载/spring-context.xml配置文件,先实例化Spring 容器,准备好Spring上下文环境。

还可以拿到DataSource dataSource数据源,用于对库直接进行操作。

如何回滚事务

单元测试执行完成后,想回滚事务,不产生垃圾数据,请在单元测试在类中加了以下注释,就可以实现

@Transactional //表示测试完成后回滚事务

SpringTransactionalContextTests基类

/**
 * Spring 单元测试基类
 * @author Admin
 * @version 2013-05-15
 */
@ActiveProfiles("production")
@ContextConfiguration(locations = {"/spring-context.xml"})
public class SpringTransactionalContextTests extends AbstractTransactionalJUnit4SpringContextTests {
	protected DataSource dataSource;
	@Autowired
	public void setDataSource(DataSource dataSource) {
		super.setDataSource(dataSource);
		this.dataSource = dataSource;
	}
	
}

一个JUnit单元测试的实例

package com.sicheng.admin.test;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sicheng.admin.sys.entity.User;
import com.sicheng.admin.test.entity.TestData;
import com.sicheng.admin.test.service.TestDataService;
import com.sicheng.common.test.SpringTransactionalContextTests;
public class TestDataServiceTest extends SpringTransactionalContextTests{
	@Autowired
	private TestDataService testDataService;
	
	@Test
	public void testInsertSelective(){
		TestData data=new TestData();
		data.setName("小王");
		data.setSex(0);
		data.setInDate(new Date());
		data.setCreateBy(new User("1"));
		data.setCreateDate(new Date());
		data.setUpdateBy(new User("2"));
		data.setUpdateDate(new Date());
		int a=testDataService.insertSelective(data);
		System.out.println(a);
		System.out.println(data.getId());
	}
	
	@Test
	public void testInsert(){
		TestData data=new TestData();
		data.setName("小王");
		data.setSex(0);
		data.setInDate(new Date());
		data.setCreateBy(new User("1"));
		data.setCreateDate(new Date());
		data.setUpdateBy(new User("2"));
		data.setUpdateDate(new Date());
		int a=testDataService.insert(data);
		System.out.println(a);
		System.out.println(data.getId());
	}
	@Test
	public void testSelectById(){
		TestData data2=testDataService.selectById("4571");
		System.out.println(data2.getId());
		System.out.println(data2.toString());
	}
	@Test
	public void testSelectByWhere(){
		TestData data=new TestData();
		data.setId("4571");
		data.setName("你好");
		data.setSex(2);
		List<TestData> list=testDataService.selectByWhere(data);
		for(TestData d:list){
			System.out.println(d.toString());
		}
	}
	@Test
	public void testSelectAll(){
		TestData data=new TestData();
		List<TestData> list=testDataService.selectAll(data);
		for(TestData d:list){
			System.out.println(d.toString());
		}
	}
	@Test
	public void testSelectAll2(){
		List<TestData> list=testDataService.selectAll(null);
		for(TestData d:list){
			System.out.println(d.toString());
		}
	}
	@Test
	public void testUpdateById(){
		TestData data=new TestData();
		data.setId("789429529165660160");
		data.setName("你好,这是修改后的名称");
		data.setSex(2);
		data.setInDate(new Date());
		data.setCreateBy(new User("1"));
		data.setCreateDate(new Date());
		data.setUpdateBy(new User("2"));
		data.setUpdateDate(new Date());
		int a=testDataService.updateById(data);
		System.out.println(a);
	}
	@Test
	public void testUpdateByWhere(){
		TestData data=new TestData();
		data.setId("789429529165660160");
		data.setName("你好,这是修改后的名称");
		data.setSex(2);
		data.setInDate(new Date());
		data.setCreateBy(new User("1"));
		data.setCreateDate(new Date());
		data.setUpdateBy(new User("2"));
		data.setUpdateDate(new Date());
		
		TestData condition=new TestData();
		condition.setId("789429529165660160");
		condition.setSex(2);
		condition.setDelFlag(null);//把del_flag设为null,表示del_flag不做为条件查询,请你根据业务情况自己把握
		int a=testDataService.updateByWhere(data,condition);
		System.out.println(a);
	}
	@Test
	public void testUpdateByIdSelective(){
		TestData data=new TestData();
		data.setId("789429529165660160");
		data.setName("你好,这是修改后的名称");
		data.setSex(2);
		data.setInDate(new Date());
		data.setCreateBy(new User("1"));
		data.setCreateDate(new Date());
		data.setUpdateBy(new User("2"));
		data.setUpdateDate(new Date());
		int a=testDataService.updateByIdSelective(data);
		System.out.println(a);
	}
	@Test
	public void testUpdateByWhereSelective(){
		TestData data=new TestData();
		data.setId("789429529165660160");
		data.setName("你好,这是修改后的名称,好的");
		data.setSex(2);
		
		TestData condition=new TestData();
		condition.setId("789429529165660160");
		condition.setSex(2);
		condition.setDelFlag(null);//把del_flag设为null,表示del_flag不做为条件查询,请你根据业务情况自己把握
		int a=testDataService.updateByWhereSelective(data,condition);
		System.out.println(a);
	}
	@Test
	public void testDeleteByWhere(){
		TestData data=new TestData();
		data.setId("789429529165660160");
		data.setName("你好,这是修改后的名称,好的");
		data.setSex(2);
		data.setDelFlag(null);//把del_flag设为null,表示del_flag不做为条件查询,请你根据业务情况自己把握
		int a=testDataService.deleteByWhere(data);
		System.out.println(a);
	}
	@Test
	public void testDeleteById(){
		int a=testDataService.deleteById("789429529165660160");
		System.out.println(a);
	}
	
	@Test
	public void testCountByWhere(){
		TestData data=new TestData();
//		data.setId("789429529165660160");
//		data.setName("你好,这是修改后的名称,好的");
		data.setSex(2);
		data.setDelFlag(null);//把del_flag设为null,表示del_flag不做为条件查询,请你根据业务情况自己把握
		int a=testDataService.countByWhere(data);
		System.out.println(a);
	}
}