---
title: "解决eclipse下运行junit测试抛出NoSuchMethodError异常的问题"
author: "joeblow"
date: "2013-03-06T09:28:01Z"
tags: []
categories: ["编程语言"]
slug: "eclipse-junit-nosuchmethoderror"
draft: false
lastmod: "2013-03-06T09:28:28Z"
id: 1823061
url: "https://joeblow.iteye.com/blog/1823061"
---

异常如下：

```java

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包。如图
![](http://dl.iteye.com/upload/attachment/0081/2031/06120e7d-0781-303e-b41b-d43f36c93af1.jpg)