解决eclipse下运行junit测试抛出NoSuchMethodError异常的问题

本文介绍了一种在Spring框架中使用EHCache时遇到版本冲突的问题及解决方案。具体表现为NoSuchMethodError错误,原因是Junit测试中加载了错误版本的EHCache库。通过调整Eclipse的调试配置,正确设置Classpath中的Bootstrap Entries,指定了正确的EHCache jar包路径来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

异常如下:

Caused by: java.lang.NoSuchMethodError: net.sf.ehcache.Cache.<init>(Ljava/lang/String;ILnet/sf/ehcache/store/MemoryStoreEvictionPolicy;ZLjava/lang/String;ZJJZJLnet/sf/ehcache/event/RegisteredEventListeners;Lnet/sf/ehcache/bootstrap/BootstrapCacheLoader;II)V
at org.springframework.cache.ehcache.EhCacheFactoryBean.createCache(EhCacheFactoryBean.java:354)
at org.springframework.cache.ehcache.EhCacheFactoryBean.afterPropertiesSet(EhCacheFactoryBean.java:331)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 38 more


原因可能是ehcache的包版本不对,但是pom.xml中确实是正确的版本,应该是eclipse运行junit时用的是另一个版本的包。

可以这样解决:
Run -> Debug Configurations... -> 选中你的junit任务 -> Classpath -> Bootstrap Entries中增加正确的jar包。如图
[img]http://dl.iteye.com/upload/attachment/0081/2031/06120e7d-0781-303e-b41b-d43f36c93af1.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值