Spring junit做单元测试时,报Failed to load ApplicationContext错误
发表于|更新于|Java
|总字数:203|阅读时长:1分钟|浏览量:
在做单元测试模块时,提示Failed to load ApplicationContext,经过查看,发现是测试单元中的一个注解指向了一个不存在的bean,该bean在applicationContext-web.xml中,而我们的代码的注解是
1 | @RunWith(SpringJUnit4ClassRunner.class) |
解决办法就是将这个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"}) |
文章作者: Charles
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 码农笔记!
相关推荐

2020-09-01
Kerberos、Impala、Druid的配合使用
本篇介绍了如何在开启Kerberos认证的情况下,数据库连接池Druid和Impala的配合使用。 背景由于生产环境开启了Kerberos的认证限制,原来可直接使用Druid连接池的方式便不可用了,需要重写Druid获取连接的逻辑,在其创建Impala连接时加入Kerberos认证的功能。 实现方式ImpalaDruidDataSource.class123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.d...

2020-07-15
利用 Spring 的类型转换器注入List属性
在Spring应用程序中,通常会为每个Spring容器(或ApplicationContext)配置一个ConversionService实例。 Spring会选择该ConversionService,然后在框架需要执行类型转换时使用它。如果未向Spring注册任何ConversionService,则使用原始的基于PropertyEditor的系统。 例如,在项目中配置Redis、ElasticSearch时,通常这些服务都是集群部署的。在创建这些bean的实例时,需要将多个节点都配置到项目的properties文件中,通常都如下所示: 12spring.elasticsearch.rest.ip-address=10.254.5.102:9200,10.254.5.107:9201,10.254.5.108:9201redis.cluster.nodes=10.10.100.197:7000,10.10.100.197:7001,10.10.100.197:7002,10.10.100.197:7003,10.10.100.197:7004,10.10.100.197:7...

2019-07-29
Spark无法使用DateTimeFormatter
今天修改Bug时发现有部分Spark的程序还在使用Date().getHours()这种过期的方法,虽然也能使用但是保不齐那天升级JDK就趟坑了,于是就想把FastDateFormat、SimpleDateFormat、Date、Calendar这些古老的处理日期的组合换成JDK1.8的LocalDate、LocalDateTime、DateTimeFormatter这种当下比较推崇的组合。 有了想法后,我就开开心心的改成了如下的代码,我大概简化为如下: 12345val standard_fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")msgRdd.foreach(line => { val arriveTime = line.split(",")(8) val dateTime = LocalDateTime.parse(arriveTime, standard_fmt) }) 启动程序后本以为一次性就搞定,结果没想到出现了一...

2023-03-21
Java和Python通过gRPC互相调用
基于前两篇,本篇介绍下如果用Java和Python通过gRPC互相调用 安装依赖123pip install grpciopip install grpcbufpip install grpcio-tools 准备.proto文件注意,该文件需要与Java项目中的.proto保持一致 1234567891011121314151617syntax = "proto3";package example;// The greeting service definition.service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {}}// The request message containing the user's name.message HelloRequest { string name = 1;}// The response messa...

2015-04-10
Idea中Tomcat乱码的问题
最近换了Windows,在Idea中启动Tomcat时会出现乱码,下面贴一下解决办法 1、在VM options中添加如下参数 1-Dfile.encoding=UTF-8 2、修改Tomcat的logging.properties 1java.util.logging.ConsoleHandler.encoding = UTF-8 替换为 java.util.logging.ConsoleHandler.encoding = GBK

2023-03-28
SpringBoot与Redis Stream整合实现消息队列
最近需要做一个简单的埋点工作,考虑到发送数据比较密集,每次都将数据实时写入那肯定不合理,于是就考虑利用消息队列做一下缓冲,避免过多的写入造成对系统的影响,这种场景拍脑门一想就是利用kafka或者rabbitmq来实现,但目前现状是申请网络策略非常麻烦,为了一个小功能再引入一个新的中间件也比较浪费,于是就想着利用redis stream来实现了。 环境要求 Redis 5.0以上,因为Redis5才新增的stream数据类型,具体可参考Redis Streams tutorial | Redis 具体实现依赖确认项目中依赖有spring-boot-starter-data-redis,如果没有的话请添加如下: 1234<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency> application...
评论
公告
开始忙了,长草期。





