在使用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. [翻译] M13BadgeView

    M13BadgeView M13BadgeView is a customizable badge view for iOS applications. The badge is built to b ...

  2. SCCM OS播发

    SCCM OS播发1.在分发点启用PXE支持2.将启动映像包分发到分发点:需要将x86和x64都分发到分发点,如果只分发x64,在客户端pxe启动时会出现 no response from wds s ...

  3. Codewars, Leetcode, Hackerrank. Online Judges Reviews

    http://jasonjl.me/blog/2015/03/30/practical-programming-practice-services/ Codewars, Leetcode, Hacke ...

  4. linux中ftp提示--553 Could not create file

    今天在阿里云的linux上搭建ftp服务的时候,搭建成功之后,上传文件时总提示553 Could not create file,找了半天原因,终于解决了 ftp主目录为/home/myftp /ho ...

  5. October 04th 2017 Week 40th Wednesday

    We teach people how to remember, we never teach them how to grow. 我们教会人们如何记忆,却从来不教他们如何成长. Without pr ...

  6. 学习python第三天之多行函数

    多行函数:(聚合函数/分组函数) 解释:多条数据进入,单条结果出来(多进单出) 1).max(obj):最大值 2).min(obj):最小值 3).sum(num):求和 4).avg(num):求 ...

  7. python里的splitlines具体解释

        Python的split方法函数能够切割字符串成列表,默认是以空格作为分隔符sep来切割字符串. In [1]: s = "www jeapedu com" In [2]: ...

  8. 【转】使用URL SCHEME启动天猫客户端并跳转到某个商品页面的方法

    在项目中遇到了这样一个需求:让用户在手机应用中,点击一个天猫的商品链接(知道商品在PC浏览器里的地址),直接启动天猫的客户端并显示这个商品.以前曾经实现过类似的功能,不过那次是淘宝的商品,天猫和淘宝的 ...

  9. jQuery.fn.extend()

    jQuery.fn.extend() extend()方法是定义在jQuery构造函数的prototype对象上面的一个方法,这样做就能使得所有jQuery对象的实例都能共享这个方法.jQuery构造 ...

  10. react中受控组件相关的warning

    在表单中,报如下的错,意思是非受控的输入框变成了受控的,报错信息如下 Warning: A component is changing an uncontrolled input of type te ...