在使用spring-test的时候,在启动@Test的方法时,spring-test会去加载spring的配置文件,这个时候如果配置文件没有在 @ContextConfiguration 中写全,就会导致加载到一半失败,然后抛出 java.lang.IllegalStateException: Failed to load ApplicationContext 的异常,进而导致优先加载的各种bean加载失败

异常:

[main] WARN org.springframework.web.context.support.GenericWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assetParser' defined in file [D:\project2\RM\target\classes\org\kinome\rm\utils\file\AssetParser.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.kinome.rm.utils.file.AssetParser]: Constructor threw exception; nested exception is java.lang.NullPointerException
[main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@f2f2cc1] to prepare test instance [MvcTest@6d469831]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)

解决方法:

只需要在 @ContextConfiguration 中把(idea)resources下的所有spring配置文件名写齐全,就不会因为加载不到没写的spring配置文件而加载失败了,写全就能成功加载完所有的配置文件进而全部加载成功了

例:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:spring-servlet.xml"}) // 这里因为没有把 applicationContext-quartz.xml 配置文件加载进去,所以导致了异常的发生
public class MvcTest {
}

只需要把 applicationContext.xml 改成 applicationContext*.xml 就可以把 applicationContext-quartz.xml 加载进来了,就能正常运行了。

以后如果再增加了新的spring文件,也得注意在spring-test的 @ContextConfiguration 中引入进来。

以后我会把spring的配置文件全部统一格式为:applicationContext(追加其他命名用来区别spring配置文件).xml ,这样一来,一个 applicationContext*.xml 就全部引入进来了,都不需要改web.xml 和 spring-test 或者其他依赖spring配置文件的需要引入的引入项 的引入了。

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext*.xml", "classpath:spring-servlet.xml"})
public class MvcTest {
}

解决 spring-test 出现 Failed to load ApplicationContext 的异常的更多相关文章

  1. java.lang.IllegalStateException: Failed to load ApplicationContext selenium 异常 解决

    WARN <init>, HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF R ...

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

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

  3. spring中junit 提示Failed to load ApplicationContext

    错误提示: 1:java.lang.IllegalStateException: Failed to load ApplicationContext 2:Error creating bean wit ...

  4. 小型SSM项目出现Failed to load ApplicationContext错误的解决方法(个人向)

    使用单元测试的时候,出现了Failed to load ApplicationContext错误,在添加了一个新的Mapper.xml文件才出现的,在保证其他配置文件没有出错的情况下,检查mapper ...

  5. SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)

    引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...

  6. SpringBoot报错:Failed to load ApplicationContext( Failed to bind properties under 'logging.level')

    引起条件: SpringBoot2.0下yml文件配置SLF4j日志输出日志级别 logging: level: debug 解决方法: 指定系统包路径 logging: root: debug 指定 ...

  7. SpringBoot报错:Failed to load ApplicationContext(Mapped Statements collection already contains value)

    错误提示: Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains v ...

  8. 集成JUnit测试错误java.lang.IllegalStateException: Failed to load ApplicationContext

    1 详细错误信息 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.t ...

  9. Spring3.x 版本和 JDK1.8 不兼容导致 java.lang.IllegalStateException: Failed to load ApplicationContext

    由于安装了 JDK1.8 的版本,最近在进行整合 Struts2+Spring+Hibernate 框架的时候,不小心导入了之前下载的 Spring 3.2.0 版本的 jar 包. 结果在运行测试用 ...

随机推荐

  1. 详细透彻解读Git与SVN的区别(集中式VS分布式)

    Git是目前世界上最先进的分布式版本控制系统,其实 Git 跟 SVN一样有自己的集中式版本库或服务器,但是Git 更倾向于被使用于分布式模式,也就是每个开发人员从中心版本库/服务器上chect ou ...

  2. 摄像机内参相关(3ds max)

    一般的原理参考:http://ksimek.github.io/2012/08/13/introduction/ 对于3ds max建模时使用的target camera,如果想得到它的内外参,可以利 ...

  3. if 的一切

    第一种语法: if 条件: # @引号是为了把条件和结果分开. 结果1 # 一个Tab或者4个空格 @告诉程序满足上面的if条件才会执行结果1结果2 如果条件为真(True),执行结果1,然后执行结果 ...

  4. Django 通过APNS推送消息

    最近手上一个项目需要通过APNS向app推送消息,由于后端采用drf框架,在github上找了好多模块,最终发现pzanitti大神的推送模块 django-push-notifications 比较 ...

  5. APP的案例分析

    很多同学有误解,软件项目管理是否就是理论课?或者是几个牛人拼命写代码,其他人打酱油的课?要不然就是学习一个程序语言,搞一个职业培训的课?都不对,软件项目管理有理论,有实践,更重要的是分析,思辨,总结. ...

  6. Angular总结三:组件

    Angular 的应用就是一棵组件树,一个页面可以是一个组件,某一页面的一个区块也可以是一个组件.为了弄明白组件及组件树,我将原来做过的一个静态网站进行组件改造. 原项目地址 https://gith ...

  7. linux下添加用户到sudo组 并禁止sudo用户修改密码

    linux下添加用户到sudo组 创建用户  useradd hanli 为新用户设置密码  passwd hanli 创建用户组  groupadd  op 将用户添加到用户组  usermod - ...

  8. 随手练——博弈论入门 leetcode - 486. Predict the Winner

    题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max ...

  9. http_load安装和使用

    一.安装 wget http://soft.vpser.net/test/http_load/http_load-12mar2006.tar.gz tar zxvf http_load-12mar20 ...

  10. Python自动化之traceback

    import traceback try: 11/a except Exception: b = traceback.format_exc() traceback.format_exc()会存储详细的 ...