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做单元测试及常见异常解决办法的更多相关文章

  1. Android Studio 常见异常解决办法

    Error:Failed to crunch file D:\Englis_installation_directory\AndroidStudio\AndroidWorkSpace\YoukAndr ...

  2. solr常见异常解决办法

    科普篇 来自百度百科:Solr简介Solr是一个基于Lucene的Java搜索引擎服务器.Solr 提供了层面搜索.命中醒目显示并且支持多种输出格式(包括 XML/XSLT 和 JSON 格式).它易 ...

  3. 使用Spring配合Junit进行单元测试的总结

    最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...

  4. spring junit 做单元测试,报 Failed to load ApplicationContext 错误

    spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...

  5. Spring集成shiro做登陆认证

    一.背景 其实很早的时候,就在项目中有使用到shiro做登陆认证,直到今天才又想起来这茬,自己抽空搭了一个spring+springmvc+mybatis和shiro进行集成的种子项目,当然里面还有很 ...

  6. Spring系列之新注解配置+Spring集成junit+注解注入

    Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...

  7. orcal 数据库 maven架构 ssh框架 的全xml环境模版 及常见异常解决

    创建maven项目后,毫不犹豫,超简单傻瓜式搞定dependencies(pom.xml 就是maven的依赖管理),这样你就有了所有你要的包 <project xmlns="http ...

  8. ClientAbortException 异常解决办法

    http://blog.sina.com.cn/s/blog_43eb83b90102ds8w.html ClientAbortException 异常解决办法 当我们用Servlet导出图片,或用J ...

  9. java启动RabbitMQ消息报异常解决办法

    启动SpringCloud微服务,RabbitMQ报如下异常: 2019-08-12 18:15:49.543 ERROR 53096 --- [68.252.131:5672] o.s.a.r.c. ...

随机推荐

  1. PHP中feof()函数的猜测

    本文环境: OS:Mac OS X 10.8.4 PHP:5.3.15 PHP的官方手册中,函数feof()下面的讨论不少,对此做了一些相关的测试. <?php print <<&l ...

  2. printf 函数原型

    typedef char *va_list; #define _AUPBND (sizeof (acpi_native_int) - 1) #define _ADNBND (sizeof (acpi_ ...

  3. 【转】localStorage使用总结

    原文地址:https://www.cnblogs.com/st-leslie/p/5617130.html 一.什么是localStorage.sessionStorage 在HTML5中,新加入了一 ...

  4. 小议SQL数据插入

    --数据插入操作:INSERT INTO user_info(username,age) VALUES('ZHANGSAN',20);INSERT INTO user_info(username,ph ...

  5. c语言第三次课

    一.const的使用1)const声明变量为只读 ; a = ; //error ] = "abcdef"; const char *p = buf; char const *p ...

  6. 一键分享代码(提供能分享到QQ空间、新浪微博、人人网等的分享功能)

    <html> <head></head> <body> <div class="xl_2"> <span styl ...

  7. SQL Data Discovery and Classification

    The new version of SQL Server Management Studio (v17.5) brings with it a new feature: SQL Data Disco ...

  8. SQL学习指南第三篇

    再谈连接 外连接 之前的范例都是没有考虑条件可能无法为表中的所有行匹配的问题 左外连接与右外连接 SELECT a.account_id, a.cust_id, b.name FROM account ...

  9. Mock5 moco框架中post请求如何加入cookies

    接着Mock4中的json文件,再往里面添加一个post 请求. 前面写法不变,后面的请求数据用的是json关键字,返回的response也是json的格式 [ { "description ...

  10. 线性布局LinearLayout

    常用属性 id:控件唯一属性 android:id="@+id/ll_1" --------------------------------------- layout_width ...