在做单元测试模块时,提示Failed to load ApplicationContext,经过查看,发现是测试单元中的一个注解指向了一个不存在的bean,该bean在applicationContext-web.xml中,而我们的代码的注解是

1
2
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")

解决办法就是将这个bean的定义写在applicationContext-test.xml中。
当然这种错误也存在其他原因,比如:项目引用了其他模块,并且有相同名称的Spring配置文件,这也会导致Failed to load ApplicationContext这个错误。

1
@ContextConfiguration(locations = { "classpath:spring*.xml", "classpath:mybatis.xml", "classpath:spring-redis.xml" })  

解决方案

1
@ContextConfiguration(locations = { "classpath*:spring*.xml", "classpath*:mybatis.xml", "classpath*:spring-redis.xml" })  

当然,偷懒也是可以滴,比如:

1
@ContextConfiguration(locations = { "classpath*:*.xml"})