spring集成Junit做单元测试及常见异常解决办法
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版本不兼容导致的,可以多试几个版本。
spring集成Junit做单元测试及常见异常解决办法的更多相关文章
- Android Studio 常见异常解决办法
Error:Failed to crunch file D:\Englis_installation_directory\AndroidStudio\AndroidWorkSpace\YoukAndr ...
- solr常见异常解决办法
科普篇 来自百度百科:Solr简介Solr是一个基于Lucene的Java搜索引擎服务器.Solr 提供了层面搜索.命中醒目显示并且支持多种输出格式(包括 XML/XSLT 和 JSON 格式).它易 ...
- 使用Spring配合Junit进行单元测试的总结
最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...
- spring junit 做单元测试,报 Failed to load ApplicationContext 错误
spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...
- Spring集成shiro做登陆认证
一.背景 其实很早的时候,就在项目中有使用到shiro做登陆认证,直到今天才又想起来这茬,自己抽空搭了一个spring+springmvc+mybatis和shiro进行集成的种子项目,当然里面还有很 ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- orcal 数据库 maven架构 ssh框架 的全xml环境模版 及常见异常解决
创建maven项目后,毫不犹豫,超简单傻瓜式搞定dependencies(pom.xml 就是maven的依赖管理),这样你就有了所有你要的包 <project xmlns="http ...
- ClientAbortException 异常解决办法
http://blog.sina.com.cn/s/blog_43eb83b90102ds8w.html ClientAbortException 异常解决办法 当我们用Servlet导出图片,或用J ...
- java启动RabbitMQ消息报异常解决办法
启动SpringCloud微服务,RabbitMQ报如下异常: 2019-08-12 18:15:49.543 ERROR 53096 --- [68.252.131:5672] o.s.a.r.c. ...
随机推荐
- php函数 array_combine
(PHP 5, PHP 7) array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 array_combine ( array $keys , array $ ...
- python dlib opencv 人脸68点特征检测
不得不感慨,现在现成的东西太多了,直接拿来用就行了 dlib安装(指定版本安装,避免踩坑) pip dlib中训练好的文件http://dlib.net/files/shape_predictor_6 ...
- rocketmq双主模式
1.官网 https://rocketmq.apache.org/ 官方安装文档 https://rocketmq.apache.org/docs/quick-start/ 2.rocketmq多主配 ...
- python爬虫爬取赶集网数据
一.创建项目 scrapy startproject putu 二.创建spider文件 scrapy genspider patubole patubole.com 三.利用chrome浏览器 ...
- Vue学习笔记5
列表渲染 用 v-for 把一个数组对应为一组元素 <div id="app"> <li v-for = "item in array"> ...
- linux中的&&,|| 与 () 命令
用&&连接两个命令,前一命令执成功(返回0)下一命令才会执行, 如 date && echo 1 会打印1,而data && echo 2不会打印2 & ...
- docker的安装,升级,与删除(最新版)
docker安装在ubuntu上 以前叫做 Docker engine安装现在叫做docker-ce的 第一种安装办法: root下执行,sudo su - root apt-get update - ...
- mpvue——componets中引入vant-weapp组件
前言 这个问题很奇葩,网上也有很多人,给了很多方法,但是我想说的是,那些方法我用了都没效果,我试了一些都没效果,因为我当时引入时报错说没有export default出来,但是一旦暴露出来就又出其他问 ...
- Codeforces Round #551 (Div. 2) 题解
CF1153A 直接做啊,分类讨论即可 #include<iostream> #include<string.h> #include<string> #includ ...
- 在Ubuntu上使用离线方式快速安装K8S v1.11.1
在Ubuntu上使用离线方式快速安装K8S v1.11.1 0.安装包文件下载 https://pan.baidu.com/s/1nmC94Uh-lIl0slLFeA1-qw v1.11.1 文件大小 ...