Spring 测试配置

来源:互联网 发布:javascript实例大全 编辑:程序博客网 时间:2024/06/06 15:32

加入jar包

<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version></dependency>
<!-- junit --><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>


import org.apache.ibatis.session.SqlSession;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.me.bean.Department;import com.me.bean.Employee;import com.me.dao.DepartmentMapper;import com.me.dao.EmployeeMapper;/** * 测试dao层的工作 推荐Spring的项目就可以使用Spring单元测试,可以自动注入我们需要的组件 1.导入SpringTest模块 * 2.ContextConfiguration指定Spring配置文件的位置 3.直接autowired要使用的组件即可 */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml")public class MapperTest {@AutowiredDepartmentMapper departmentMapper;@AutowiredEmployeeMapper employeeMapper;@AutowiredSqlSession sqlSession;/** * 测试Department */@Testpublic void testCURD() {// //1.创建SpringIoc容器// ApplicationContext ioc = new// ClassPathXmlApplicationContext("applicationContext.xml");// //2.从容器得到mapper// DepartmentMapper bean = ioc.getBean(DepartmentMapper.class);System.out.println(departmentMapper);// 插入部门departmentMapper.insertSelective(new Department(null, "测试部"));departmentMapper.insertSelective(new Department(null, "开发部"));// 生成员工测试employeeMapper.insertSelective(new Employee(null, "jerry", "M", "jerry@qq.com", 1));// 批量插入员工:批量,可以执行批量操作的sqlSessionEmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);for (int i = 0; i < 100; i++) {String uid = UUID.randomUUID().toString().substring(0, 8);mapper.insertSelective(new Employee(null, uid, "M", uid + "@qq.com", 1));}}@Testpublic void testSelect(){Employee selectByPrimaryKeyWithDept = employeeMapper.selectByPrimaryKeyWithDept(1);System.out.println(selectByPrimaryKeyWithDept);}}
模拟http请求测试

要加入Servlet3.0

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency


/** *使用Spring测试模块提供的测试请求功能,测试crud请求的正确性 *Spring4测试的时候,需要servlet3.0支持 */@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration//file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml src 前不要带 “/”@ContextConfiguration(locations = {"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})public class MvcTest {//传入Springmvc的ioc 需要注解 @WebAppConfiguration@AutowiredWebApplicationContext context;//虚拟mvc请求,获取到处理结果MockMvc mockMvc;@Beforepublic void initMockMvc(){mockMvc = MockMvcBuilders.webAppContextSetup(context).build();}@Testpublic void testPage() throws Exception{//模拟请求拿到返回值MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();//请求成功以后,请求域中会有pageInfo:我们可以取出来pageInfo进行验证MockHttpServletRequest request = result.getRequest();PageInfo pi =(PageInfo) request.getAttribute("pageInfo");System.out.println("当前页码:"+pi.getPageNum());System.out.println("总页数:"+pi.getPages());System.out.println("每页显示:"+pi.getPageSize());System.out.println("总记录数:"+pi.getTotal());System.out.println("在页面上连续显示的页码:");List list = pi.getList();int[] nums = pi.getNavigatepageNums();for (int num : nums) {System.out.print(num+ "");}}}



原创粉丝点击