一.springTest使用
1.导入springTest模块
<!-- spring-test 还需要引入junit -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2.springTest 注入bean示例
import com.miracle.beans.Employee;
import com.miracle.dao.EmployeeMapper;
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;
/**
* @ContextConfiguration 注解的 locations指定spring配置文件的位置
*
* @RunWith(SpringJUnit4ClassRunner.class) 指定使用哪种单元测试
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Test2 {
// 这里可以直接注入依赖
@Autowired
EmployeeMapper employeeMapper;
@Test
public void t1(){
Employee employee = employeeMapper.selectByPrimaryKeyWithDept(3);
System.out.println(employee);
}
}
3.springTest 模拟页面请求
package com.miracle;
import com.github.pagehelper.PageInfo;
import com.miracle.beans.Employee;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.List;
/**
* 要想使用springTest 需要3.0以上版本的Servlet
*
* @ContextConfiguration 注解的 locations指定spring配置文件的位置,由于要测试请求,还要加上springMVC配置文件
*
* 加载配置文件路径还可以写 : file:src/main/webapp/WEB-INF/dispatcher-servlet.xml
*
* @RunWith(SpringJUnit4ClassRunner.class) 指定使用哪种单元测试
*/
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:dispatcher-servlet.xml"})
public class Test2 {
// 虚拟的mvc请求,可以模拟浏览器请求
private MockMvc mockMvc;
/**
* WebApplicationContext 为 springMVC的IOC容器
*
* 这里使用 @Autowired 注解自动装配,但是@Autowired只能装配IOC中的bean,
* 无法装配IOC容器本身,需要在类上加额外配置 @WebAppConfiguration
*/
@Autowired
private WebApplicationContext webApplicationContext;
// 初始化 MockMvc
@Before
public void initMockMvc(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void test1() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pageNum", "1")).andReturn();
// 请求成功以后,请求域中会有数据,去请求域拿数据验证
MockHttpServletRequest request = mvcResult.getRequest();
PageInfo pageInfo = (PageInfo)request.getAttribute("pageInfo");
System.out.println("当前页码:" + pageInfo.getPageNum());
System.out.println("总页码:"+ pageInfo.getPages());
System.out.println("总记录数:"+ pageInfo.getTotal());
int[] nums = pageInfo.getNavigatepageNums();
for (int num : nums) {
System.out.println("在页面需要连续显示的页码:"+ num);
}
// 获取员工信息
List<Employee> list = pageInfo.getList();
for (Employee employee : list) {
System.out.println(employee);
}
}
}