不知道為什么,Junit測試在我的設(shè)備上面一直無法運(yùn)行胡桨。反正學(xué)習(xí)的項(xiàng)目幾乎都是Spring項(xiàng)目官帘,索性我就嘗試一下SpringTest測試套件。沒想到反而可以實(shí)現(xiàn)代碼的單元測試昧谊。
前期準(zhǔn)備
所有的java項(xiàng)目都是在引入jar包開始的刽虹。我這里使用的是maven項(xiàng)目所以,我所說的都是在maven環(huán)境下進(jìn)行的呢诬。如果沒有安裝請自行百度涌哲。
pom.xml配置詳解請參考博客:http://blog.csdn.net/ithomer/article/details/9332071
1.引入maven依賴
在pom.xml文件中導(dǎo)入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.創(chuàng)建MapperTest.java文件(Test類)
@SuppressWarnings("unused")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class MapperTest {
/* //1.由于已經(jīng)在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, "開發(fā)部"));
// departmentMapper.insertSelective(new Department(null, "創(chuàng)意部"));
// 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.批量插入員工信息,使用可以執(zhí)行批量插入SQLSession
/*for () {
在applicationContext.xml配置一個(gè)可以執(zhí)行批量插入的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("批量插入完成狗唉!");
}
}
以上只是實(shí)現(xiàn)的偽代碼:但是需要注意的如下
要實(shí)現(xiàn)初烘,SpringTest測試套件除了引入依賴還需要再Test類上加入如下注解
1. @RunWith(SpringJUnit4ClassRunner.class)
這個(gè)注解表示的是運(yùn)行單元測試的時(shí)候,可以使用哪個(gè)單元測試類分俯,來運(yùn)行账月。
2. @ContextConfiguration(locations={"classpath:applicationContext.xml"})
使用這個(gè)注解指定了Spring配置文件的位置,他會自動幫助我們創(chuàng)建IOC容器澳迫,省略了
手動創(chuàng)建的過程
ApplicationContext ioc = new ClassPathXmlApplicationContext();
這個(gè)步驟不在需要手動創(chuàng)建
除了要實(shí)現(xiàn)以上內(nèi)容以外局齿,如果在Test類中對一些類進(jìn)行測試,只需要相應(yīng)的類上面加入 @Autowired 注解即可
@Autowired
DepartmentMapper departmentMapper;
@Autowired
EmployeeMapper employeeMapper;
// 做批操作
@Autowired
SqlSession sqlSession;