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. ...
随机推荐
- L2-2 重排链表 (25 分)
给定一个单链表 L1→L2→⋯→Ln−1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln−1→L2→⋯.例如:给定L为1→2→3→4→5→6,则输出 ...
- ARC089E GraphXY 构造
传送门 在Luogu上评了"NOI"之后评级变成了"普及+/提高"--我觉得我可能要退群了 考虑构造一个这样的图: 其中上半部分是从\(S\)开始的一条长\(1 ...
- Python Revisited Day 05(模块)
目录 5.1 模块与包 5.1.1 包 5.2 Python 标准库概览 5.2.1 字符串处理 io.StringIO 类 5.2.3 命令行设计 5.2.4 数学与数字 5.2.5 时间与日期 5 ...
- Cinder组件
cinder-api cinder-api 是整个 Cinder 组件的门户,所有 cinder 的请求都首先由 cinder-api 处理. cinder-api 向外界暴露若干 HTTP REST ...
- [ffmpeg] 音频样本
不仅限于ffmpeg,音频采样所得的PCM都含有三个要素:声道(channel).采样率(sample rate).样本格式(sample format). 声道 当人听到声音时,能对声源进行定位,那 ...
- mongoDB 文档操作_改
mongoDB 更改操作 格式对比 MySQL update table set .... where .... db.collection.updateOne(query,update,upsert ...
- Input标签使用整理
0 写在前面 对于程序而言I/O是一个程序的重要组成部分.程序的输入.输出接口,指定了程序与用户之间的交互方式.对于前端开发而言,input标签也有着其重要地位,它为用户向服务端提交数据提供了可能. ...
- vue 中 echart 在子组件中只显示一次的问题
问题描述 一次项目开发过程中,需要做一些图表,用的是百度开源的 echarts. vue推荐组件化开发,所以就把每个图表封装成子组件,然后在需要用到该图表的父组件中直接使用. 实际开发中,数据肯定都是 ...
- 为什么会有这么多python?其实python并不是编程语言!
Python是出类拔萃的 然而,这是一句非常模棱两可的话.这里的"Python"到底指的是什么? 是Python的抽象接口吗?是Python的通用实现CPython吗(不要把CPy ...
- Day040--HTML&CSS
内容回顾: 标签分类: (1)行内标签 span 小跨度的标签 i em a 特点: (1)在一行内显示 (2)不能设置宽高,如果不设置宽高,默认是内容的宽高 (2)块级标签 h1~h6 h1页面中尽 ...