单元测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring/*.xml" })
public class UserTest {@AutowiredUserService userService;@Testpublic void testUser(){System.out.println("单元测试");//查询一个用户信息UserVo user = userService.getUser(1);System.out.println(user.getName());}
}

运行报错

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0’: Invocation of init method failed; nested exception is java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler [locations=[class path resource [WEB-INF/css/]],resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@557a1e2d]]] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@62ee68d8: startup date [Sun Jul 30 18:48:01 CST 2017]; root of context hierarchy

错误提示静态资源css创建有问题,静态资源Handler的ResourceHttpRequestHandler不能创建

解决:添加注解@WebAppConfiguration(“src/main/resources”)

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/main/resources")
@ContextConfiguration(locations = { "classpath*:spring/*.xml" })
public class UserTest {@AutowiredUserService userService;@Testpublic void testUser(){System.out.println("单元测试");//查询一个用户信息UserVo user = userService.getUser(1);System.out.println(user.getName());}
}

这个注解的作用就是指定到配置文件的根路径

官方文档

@WebAppConfiguration is a class-level annotation that is used to declare that the ApplicationContext loaded for an integration test should be a WebApplicationContext.The presence of @WebAppConfiguration on a test class indicates that a WebApplicationContext should be loaded for the test using a default for the path to the root of the web application. To override the default, specify an explicit resource path via the value() attribute.
Note that @WebAppConfiguration must be used in conjunction with @ContextConfiguration, either within a single test class or within a test class hierarchy.@WebAppConfiguration是一个类级别的注释,用于声明ApplicationContext为集成测试加载的应该是a WebApplicationContext。
@WebAppConfiguration测试类的存在表明WebApplicationContext应该使用Web应用程序根路径的默认值为测试加载a。要覆盖默认值,请通过该value()属性指定显式资源路径。请注意,@WebAppConfiguration必须与 @ContextConfiguration单个测试类或测试类层次结构一起使用。
从Spring Framework 4.0开始,此批注可用作 元注释来创建自定义组合注释。

单元测试Error creating bean with name org.springframework.web.servlet.resource.Resource相关推荐

  1. Error creating bean with name ‘org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#0‘

    今天跑一个SpringMVC的web项目,刚运行tomcat就报出500,根据网上查的诸多资料进行修改都没能解决. 运行日志输出报的错误: javax.servlet.ServletException ...

  2. Error creating bean with name 'org.springframework.amqp.rabbit.config.ListenerContainerFactoryBean#0

    Spring 整合rabbitmq 出现错误 严重: Exception sending context initialized event to listener instance of class ...

  3. Error creating bean with name ‘org.springframework.security.oauth2.config.annotation.web.configurati

    出现以下错误主要原因是加入了资源服务器的配置却没有标识该服务为资源服务器. org.springframework.beans.factory.BeanCreationException: Error ...

  4. 启动springboot报错Error creating bean with name 'dataSource' defined in class path resource

    2019独角兽企业重金招聘Python工程师标准>>> 启动springboot报错Error creating bean with name 'dataSource' define ...

  5. BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [

    现场情景: 初次搭建springboot工程,原本要搭建一个springboot+mybatis的maven工程,听说springboot会把一切给配置好,天真的在执行完mybatis自动生成mapp ...

  6. Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource

    1.问题描述 在redis整合springcache的时候一直报错. 按照信息找的发生问题的部分: @Configuration @EnableCaching public class RedisCo ...

  7. Error creating bean with name 'dataSource' defined in class path resource [spring/spring-dao.xml]:

    Error creating bean with name 'dataSource' defined in class path resource [spring/spring-dao.xml]: B ...

  8. 解决Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource...问题

    文章目录 1. 复现问题 2. 分析问题 3. 解决问题 1. 复现问题 今天在启动spring boot项目时,出现如下错误: org.springframework.beans.factory.B ...

  9. Error creating bean with name ‘sqlSessionFactory‘ defined in class path resource [applicationContext

    学习spring框架时遇到一个问题,找了很久. 记录一个错误, D:\Java\jdk-14.0.2\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1 ...

最新文章

  1. 顶部标题栏(四)自定义ActionBar风格和样式
  2. 前端div里的内容下沉_自学Web前端的五个不同阶段,从浅入深
  3. nodejs应用部署到SAP云平台的经过
  4. 人工智能安全框架(2020年)
  5. mysql初级知识梳理与练习
  6. java套接字实验总结,实验三、WINSOCK套接字编程实验报告
  7. Linux/Centos: readelf命令使用说明
  8. 【虚拟主播】刚刚,我用三行代码创建了一个虚拟主播
  9. Charles青花瓷抓包
  10. 有效集法(Active Set),内点法(Interior-Point)及序列二次规划(SQP)法
  11. 360视频:旋转球面投影RSP
  12. python函数之任意数量的实参
  13. android rfid 定位,基于Android手机的室内定位技术研究与实现
  14. mac上Apk反编译工具合集整理与资源
  15. 将数据生成带图表的pdf,并下载
  16. 科技爱好者周刊:第 73 期
  17. Linux下安装最新版Elasticsearch
  18. [中医养生] 听JT叔叔讲中医基础理论 第三讲
  19. sicktim571操作手册_TIM3xx简明操作手册
  20. OA办公系统如何实现合同管理

热门文章

  1. Ubuntu的LAMP与相关软件安装设置
  2. JVM 内存管理、自带性能监测调优工具 (jstack、jstat)及 JVM GC 调优
  3. Python web —— Selenium 库
  4. Python Tricks(九)—— 递归遍历目录下所有文件
  5. Tricks(三十四)—— 判断某一属性列是数值型还是标称型
  6. 【剑指 offer】(48)—— 不能被继承的类
  7. Python基础——yield的使用与解释
  8. java数字常量_数字常量 - javawebsoa - 博客园
  9. php usort的用法,PHP usort()用法及代码示例
  10. 个人博客网页设计_不会代码如何打造个人博客?你需要这个简单、免费的搭建工具...