不知道为什么,Junit测试在我的设备上面一直无法运行。反正学习的项目几乎都是Spring项目,索性我就尝试一下SpringTest测试套件。没想到反而可以实现代码的单元测试。
前期准备
所有的java项目都是在引入jar包开始的。我这里使用的是maven项目所以,我所说的都是在maven环境下进行的。如果没有安装请自行百度。
pom.xml配置详解请参考博客:http://blog.csdn.net/ithomer/article/details/9332071
1.引入maven依赖
在pom.xml文件中导入maven依赖,分别包含Junit 和 SpringTest
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- SpringTest测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.4.RELEASE</version>
<scope>test</scope>
</dependency>
由于,SpringTest测试套件也是基于Junit的,所以必须引入Junit的依赖
2.创建MapperTest.java文件(Test类)
@SuppressWarnings("unused")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class MapperTest {
/* //1.由于已经在xml中配置了Spring映射,可以从Spring IOC容器中拿到mapper
ApplicationContext ioc = new ClassPathXmlApplicationContext();
//2.从容器中获取mapper
Department department = ioc.getBean(Department.class);
// department.getDeptId()
System.out.println(department.getDeptId().toString());*/
System.out.println(departmentMapper);
// departmentMapper.insertSelective( new Department(null, "开发部"));
// departmentMapper.insertSelective(new Department(null, "创意部"));
// departmentMapper.insertSelective(new Department(null, "销售部"));
// employeeMapper.insertSelective(new Employee(null, "张三丰", "男", "lightsnowliu@qq.com", 1));
// employeeMapper.insertSelective(new Employee(null, "张无忌", "男", "123412312@qq.com", 2));
// employeeMapper.insertSelective(new Employee(null, "小昭", "女", "2342345234@gmail.com", 3));
// employeeMapper.insertSelective(new Employee(null, "赵敏", "女", "asdfsadfadsg@gmail.com", 2));
// employeeMapper.insertSelective(new Employee(null, "周芷若", "女", "asdfff@gmail.com", 1));
// employeeMapper.insertSelective(new Employee(null, "李莫愁", "女", "limochou@gmail.com", 3));
// 3.批量插入员工信息,使用可以执行批量插入SQLSession
/*for () {
在applicationContext.xml配置一个可以执行批量插入的SQLsession
}*/
@Autowired
DepartmentMapper departmentMapper;
@Autowired
EmployeeMapper employeeMapper;
// 做批操作
@Autowired
SqlSession sqlSession;
/*
* 测试department的信息
*/
@Test
public void testMembers(){
int max=6;
int min=1;
Random random = new Random();
int s = random.nextInt(max)%(max-min+1) + min;
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
for (int i = 0; i < 10; i++) {
String uid = UUID.randomUUID().toString().substring(0, 5)+i;
mapper.insertSelective(new Employee(null, uid, "女", uid + "@gmail.com", 2));
}
System.out.println("批量插入完成!");
}
}
以上只是实现的伪代码:但是需要注意的如下
要实现,SpringTest测试套件除了引入依赖还需要再Test类上加入如下注解
1. @RunWith(SpringJUnit4ClassRunner.class)
这个注解表示的是运行单元测试的时候,可以使用哪个单元测试类,来运行。
2. @ContextConfiguration(locations={"classpath:applicationContext.xml"})
使用这个注解指定了Spring配置文件的位置,他会自动帮助我们创建IOC容器,省略了
手动创建的过程
ApplicationContext ioc = new ClassPathXmlApplicationContext();
这个步骤不在需要手动创建
除了要实现以上内容以外,如果在Test类中对一些类进行测试,只需要相应的类上面加入 @Autowired 注解即可
@Autowired
DepartmentMapper departmentMapper;
@Autowired
EmployeeMapper employeeMapper;
// 做批操作
@Autowired
SqlSession sqlSession;