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+ "");}}}
阅读全文
0 0
- Spring 测试配置
- spring mvc测试之配置
- Junit4,Spring集成测试配置
- Spring MVC配置及测试
- 基于Spring测试的jndi配置问题
- spring 2.5+junit4.4 测试基础配置
- spring hsqldb 测试潜入数据库 配置程序
- cxf的spring启动测试配置
- Spring整合Junit测试,并且配置事务
- JUNIT -- 调用spring配置进行测试
- spring dubbo junit4 测试类配置
- Spring-注解配置错误(测试错误)
- Java Spring搭配Junit测试配置
- Java Spring搭配TestNG测试配置
- Spring Mvc的xml配置和测试
- spring+hibernate测试和产品数据库不同的配置
- 用JUnit测试Spring中的配置,需要使用ApplicationContext
- spring声明式事务配置是否成功测试方法
- hdu 1286 找朋友(筛选)
- windows环境下python安装open cv 读取tiff文件
- NOIP2015提高组解题报告
- Js学习之路五 --- 选择框(全选、全不选、反选)
- 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交 换,输出数组
- Spring 测试配置
- 2016 ACM-ICPC Asia Regional Changchun 1006 Harmonic Value Description(逻辑思维)
- Java中接口的实际应用:指定操作的标准
- Android开发——数据库框架Suger遇到的大坑(Gson和Suger的复用Bean请见“大坑三”)
- c/c++面试题
- 镜子迷宫记录
- LeetCode-Reconstruct Original Digits from English
- actionscript3.0离线帮助文档下载
- html块级元素、行级元素、内联块级元素的特点