spring-test 依赖包
- <!--Spring-test -->
- <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>4.3.7.RELEASE</version>
- </dependency>
1, 简单单元测试
- package com.ssm.test;
- import java.util.List;
- import java.util.Map;
- 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.ssm.dao.UserMngDao;
- /**
- * @author wangxiangyu
- * @date:2017 年 7 月 18 日 下午 2:24:50
- * 类说明: Spring 单元测试
- * 1, 导入 spring-test 单元测试的 jar 包
- * 2,@ContextConfiguration(locations={"classpath:applicationContext.xml"}) 指定 Spring 配置文件的位置
- * 3,@RunWith(SpringJUnit4ClassRunner.class) 使用 Spring 单元测试
- * 3, 直接 autowired 要使用的组件
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext.xml"})
- public class MapperTest {
- @Autowired
- UserMngDao userMngDao;
- @Test
- public void test1(){
- List<Map> users = userMngDao.findAll();
- for(Map user : users){
- String staffName = null==user.get("staffName")?"":user.get("staffName").toString();
- System.out.println(staffName);
- }
- }
- }
2, 模拟前端请求单元测试
- package com.atguigu.crud.test;
- import java.util.List;
- 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 com.atguigu.crud.bean.Employee;
- import com.github.pagehelper.PageInfo;
- /**
- * 使用 Spring 测试模块提供的测试请求功能, 测试 curd 请求的正确性
- * Spring4 测试的时候, 需要 servlet3.0 的支持
- * @author lfy
- *
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @WebAppConfiguration
- @ContextConfiguration(locations = { "classpath:applicationContext.xml", "file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml" })
- public class MvcTest {
- // 传入 Springmvc 的 ioc
- @Autowired
- WebApplicationContext context;
- // 虚拟 mvc 请求, 获取到处理结果.
- MockMvc mockMvc;
- @Before
- public void initMokcMvc() {
- mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
- }
- @Test
- public void testPage() throws Exception {
- // 模拟请求拿到返回值
- MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).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.getTotal());
- System.out.println("在页面需要连续显示的页码");
- int[] nums = pi.getNavigatepageNums();
- for (int i : nums) {
- System.out.print(" "+i);
- }
- // 获取员工数据
- List<Employee> list = pi.getList();
- for (Employee employee : list) {
- System.out.println("ID:"+employee.getEmpId()+"==>Name:"+employee.getEmpName());
- }
- }
- }
3,Junit 常见异常及解决办法
异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
解决方法:
java1.8 版本只支持 spring4.0 以上. 所以解决方法有两种: 1) 把 sping 版本换成 4.0 以上; 2) 把 jdk 调低点. 此处我选择把 jdk 调低点, 再次运行.
异常:
java.lang.NoClassDefFoundError: org/junit/runners/model/MultipleFailureException
解决方法:
没有 multipleFailureException 类, 可能是因为你的版本过低引起的, 请更换最新版本.
异常:
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test_sayHello4]
解决方法:
Spring-test 版本与 junit 版本不兼容导致的, 可以多试几个版本.
来源: https://www.cnblogs.com/xyhero/p/9434252.html