spring 单元测试及其错误整理

目录:

  • NO1 spring单元测试方法

    - NO1.1 pom.xml文件中确认有下面依赖

    - NO1.2 在需要测试的类上,或者新建的测试类上添加注解

    - NO1.3 注解说明
  • NO2 spirng单元测试错误整理

    - NO2.1 java.lang.IllegalStateException: Failed to load ApplicationContext

    - NO2.2 java.lang.NoSuchMethodError: org.springframework.util.Assert.notNull

    - NO2.3 The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved

NO1 spring 单元测试方法

NO1.1 pom.xml文件中确认有下面的依赖

        <!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring单元测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<!--确保版本与其他spring开头的依赖相同 -->
<version>5.0.6.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<!--确保版本与其他spring开头的依赖相同 -->
<version>5.0.6.RELEASE</version>
</dependency>

NO1.2 在需要测试的类上,或者新建的测试类上添加注解

package com.authrization;

import com.authrization.dao.RoleDao;
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 org.springframework.test.context.web.WebAppConfiguration; /**
* @author weicong
* @data 2018/6/1 12:02
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:config/spring*.xml"})
@WebAppConfiguration
public class TestBBB { @Autowired
RoleDao roleDao; @Test
public void testddd(){ System.out.println(roleDao.listRoles());
}
}

NO1.3 注解说明

(1)如果 @ContextConfiguration 有多个xml文件可以这样写

@ContextConfiguration(locations = {"classpath*:config/spring-context*.xml","classpath*:config/spring-mvc.xml"})

(2)有些web项目中不用加 @WebAppConfiguration 也可以运行单元测试

NO2 spirng 单元测试错误整理

NO2.1 java.lang.IllegalStateException: Failed to load ApplicationContext

可能原因:

(1)缺乏 servlet-api 依赖

		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>

(2)测试类上缺乏 @WebAppConfiguration 注解

NO2.2 java.lang.NoSuchMethodError: org.springframework.util.Assert.notNull

可能原因:

(1)**spirng-test ** 依赖版本与spring全家桶(spring-core、spring-beans、spring-web等等)的依赖版本相差太大,尽量保持spring系列依赖版本全部相同

参看:

https://stackoverflow.com/questions/28013452/could-not-load-testcontextbootstrapper-spring-unit-testing

(2)重复的依赖

NO2.3 java.lang.NoClassDefFoundError: org/springframework/core/ErrorCoded

可能原因:

(1)缺乏 spring-core 依赖

NO2.3 The import org.springframework.test.context.junit4.SpringJUnit4ClassRunner cannot be resolved

原因:maven依赖指定了在test范围才生效

解决:去掉test

spring 单元测试方法及其错误整理的更多相关文章

  1. myBatis+Spring+SpringMVC框架面试题整理

    myBatis+Spring+SpringMVC框架面试题整理(一) 2018年09月06日 13:36:01 新新许愿树 阅读数 14034更多 分类专栏: SSM   版权声明:本文为博主原创文章 ...

  2. spring 以及 spring mvc 中常用注解整理

    spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...

  3. Python编程:基础学习常见错误整理

    # Python学习之错误整理: # 错误一:# TypeError: cannot concatenate 'str' and 'int' objects# 不能连接str和int对象age = 2 ...

  4. (6)Spring Boot web开发 --- 错误处理页面

    文章目录 处理时间(`Date`)类型 thymeleaf 页面拼接字符串 映射路径占位符 使用 put.delete 方法 错误处理机制 处理时间(Date)类型 Spring Boot 进行参数绑 ...

  5. Spring SpringMVC SpringBoot SpringCloud 注解整理大全

    Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...

  6. SSH(struts+spring+hibernate)常用配置整理

    SSH(struts+spring+hibernate)常用配置整理 web.xml配置 <?xml version="1.0" encoding="UTF-8&q ...

  7. java 面向对象(十七):单元测试方法

    * Java中的JUnit单元测试 * * 步骤: * 1.中当前工程 - 右键择:build path - add libraries - JUnit 4 - 下一步 * 2.创建Java类,进行单 ...

  8. spring开发中常见错误集合,逐步添加

    1.关于jstl错误:原因,在jsp页面中使用了jstl标签库,但是却没有导入,可以将相应的jar包放在tomcat的lib目录下,一劳永逸 Java.lang.NoClassDefFoundErro ...

  9. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(三)(错误整理篇)

    使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二) 以上两篇已经把流 ...

随机推荐

  1. 用python计算圆周率PI

    1.蒙特卡洛求圆周率 向区域内随即撒点 当点的数目足够多时,落在圆的点数目与在正方形点数目成正比 即圆的面积和正方形的面积成正比 可以得出计算圆周率的算法 DARTS=100000000   hits ...

  2. Odoo domain 中的 like, ilike, =like, =ilike

    Odoo domain 中的 like, ilike, =like, =ilike 举例说明[转]   Odoo domain 中的 like, ilike, =like, =ilike Odoo d ...

  3. Spring+Quartz集群环境下定时调度的解决方案

    集群环境可能出现的问题 在上一篇博客我们介绍了如何在自己的项目中从无到有的添加了Quartz定时调度引擎,其实就是一个Quartz 和Spring的整合过程,很容易实现,但是我们现在企业中项目通常都是 ...

  4. Ruby http/net 中连接超时问题

    下面在调用币安的接口时,经常会卡住,设置连接超时也不会抛出异常,代码如下(默认连接超时为nil, 参考:https://github.com/ruby/ruby/pull/269): require ...

  5. pycharm 有些模块没有提示 解决方法

    解决方法:右键模块所在文件夹,选择make directory as ,选择excluded或者sources root,即可.

  6. ERRORS !MySQL 和 Javaweb 的报错合集

    ERROR:1175. You are using safe update mode and you tried to update a table without a WHERE that uses ...

  7. spring微服务架构-脑图

    spring团队对新一代软件开发的思索.为什么软件开发是spring boot?为什么软件开发是spring cloud?如何使用spring cloud搭建微服务. 清晰脑图查看

  8. php实现仿百度分页

    本文主要和大家分享php实现仿百度分页的实例,先来看一下效果图. 基本策略 1, 首先要查询总记录数 2, 设置每页显示的数量并计算总页数 3, 增加一个get传值num代表当前访问的是哪一页,如果没 ...

  9. hdu3307 欧拉函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3307 Description has only two Sentences Time Limit: 3 ...

  10. lllll