我在Mac 10.9.1,Maven 3.1.1,JUnit 4.11上使用Eclipse Juno,并且具有此Apache httpcomponents依赖性:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
</dependency>
当我在Eclipse中运行JUnit测试时(通过右键单击我的测试名称,并选择“Run As” - &gt;“Junit Test”),我收到以下错误
java.lang.NoSuchMethodError: org.apache.http.impl.client.DefaultHttpClient.execute(Lorg/apache/http/client/methods/HttpUriRequest;)Lorg/apache/http/client/methods/CloseableHttpResponse;
at org.usersubscr.subco.ebook.mvc.CleverInstantLoginController.validateInstantAccessLogin(CleverInstantLoginController.java:143)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.mainco.subco.ebook.mvc.CleverInstantLoginControllerTest.submitLoginWithCode(CleverInstantLoginControllerTest.java:119)
at org.mainco.subco.ebook.mvc.CleverInstantLoginControllerTest.testInstantLoginSuccess(CleverInstantLoginControllerTest.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
事情正在消亡的具体路线是
response1 = httpclient.execute(httppost);
令人沮丧的是,当我在命令行上运行相同的测试时,使用
mvn clean test -Dtest=MyHttpTest
测试运行良好,没有错误。我尝试在我的项目上运行“mvn eclipse:eclipse”并刷新我的工作区,但这似乎没有帮助。如何使Eclipse JUnit的行为与在命令行上运行测试的方式相同?
答案 0 :(得分:0)
我经常遇到这个问题。
如果我是从命令行或从Eclipse运行Maven,那么我总是mvn clean test
。
如果我从Eclipse运行JUnit,那么我首先执行Project&gt;清理(然后是构建),最后运行为JUnit。如果仍然无法解决问题,那么我首先进行Maven Clean并重复此过程。
底线是:Maven和Eclipse各自对世界如何运作有自己的(强烈的)意见,在某些情况下它们相互矛盾。
答案 1 :(得分:0)
好的,与
有同样的问题java.lang.NoSuchMethodError: org.apache.commons.collections.MapUtils.isNotEmpty
对我而言,这是因为Hibernate包含commons-collections-3.1,其中不包含方法isNotEmpty()。我虽然这很奇怪,因为当我使用m2e插件依赖层次结构功能时,它向我显示它会自动排除依赖关系并使用3.2代替。更糟糕的是,似乎从pom中手动排除神器并没有做任何事情。
对我来说问题是我有Maven依赖,并且范围设置为test,并且出于某种原因Eclipse JUnit使用此依赖项而没有异常。我有多模块项目,我试图找出下游模块中的问题(也就是说,我正在寻找包含模块的模块的依赖层次结构,其中包含另一个模块,它具有导致问题的依赖性),所以由于范围是测试,Eclipse Maven插件没有看到依赖项。如上所述,JUnit不明白它不应该在未来使用这种依赖性,这让我很头疼。
对于你来说问题可能是别的,所以我试着给出更一般的图片来追踪问题。以下是我用来跟踪和消除问题的过程,希望这会有所帮助:
检查确切的pom文件并排除罪魁祸首(对我而言,它是以下内容:)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
</exclusions>
</dependency>