当我在 Eclipse 中运行测试时,出现 NoSuchMethodError: org.hamcrest.Matchers.hasXPath
我有一个使用 hamcrest 库 (1.2) 的单元测试。重要的是它是 1.2,因为我想在 hasXPath 匹配器中包含命名空间上下文。这是一个 Maven 项目,我已将所有依赖项设置为正常工作。 (我确保我只使用 junit-dep 而不是 junit - 很痛苦,但我已经确认我的依赖树是正确的。)在 Maven 中一切正常。但是,当我在 eclipse (3.6) 中运行相同的测试时,出现以下错误:
java.lang.NoSuchMethodError: org.hamcrest.Matchers.hasXPath(Ljava/lang/String;Ljavax/xml/namespace/NamespaceContext;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher;
at com.factorlab.ws.obs.meta.PhenomononGroupsResourceITest.testGetPhenomenonGroupsXml(PhenomononGroupsResourceITest.java:36)
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.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
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)
我已仔细检查了该项目的 eclipse 构建路径配置,它也有 junit-dep,但没有 junit 和 hamcrest 1.2。此外,没有编译错误,所以这只是运行时的事情。 Eclipse 是否有自己的运行时会干扰这里?我该如何解决这个问题?
导致异常的代码在这里:
private NamespaceContext namespaceContext = new MetaNamespaceContext();
@Test
public void testGetPhenomenonGroupsXml() throws Exception {
WebClient webClient = new WebClient();
webClient.addRequestHeader("Accept", "application/xml");
XmlPage xmlResult = webClient.getPage(BASE_URL);
//printDoc(xmlResult.getXmlDocument(), System.out);
assertThat("count of groups",
xmlResult.getXmlDocument(),
hasXPath("count(/phenomenonGroups/om:phenomenonGroup)",
namespaceContext, equalTo("4")));
assertThat("first group",
xmlResult.getXmlDocument(),
hasXPath(
"/phenomenonGroups/om:phenomenonGroup/om:quickYesNoPhenomenon/id/text()",
namespaceContext, equalTo("1")));
}
我不确定在没有大量上下文的情况下这是否有帮助 - 即 Web 服务代码、完整的类代码、MetaNamespaceContext 等依赖类。但是,我有信心,因为这当我从命令行执行 mvn clean install
时,一切正常,这一定是 Eclipse 配置问题,与正在运行的特定代码无关。 (当然,除了 hasXPath(String, NamespaceContext, Matcher)
方法仅在 1.2 中可用,如果我只使用 hasXPath(String, Matcher)
一切都会正常工作>。
I have a unit test that uses the hamcrest library (1.2). It's important that it's 1.2 because I want to include a namespace context in the hasXPath matcher. This is a maven project and I have all my dependencies set up to work correctly. (I make sure that I only use junit-dep and not junit - a pain but I've confirmed that my dependency tree is correct.) Everything works fine in maven. However, when I run the same test in eclipse (3.6) I get the following error:
java.lang.NoSuchMethodError: org.hamcrest.Matchers.hasXPath(Ljava/lang/String;Ljavax/xml/namespace/NamespaceContext;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher;
at com.factorlab.ws.obs.meta.PhenomononGroupsResourceITest.testGetPhenomenonGroupsXml(PhenomononGroupsResourceITest.java:36)
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.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
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.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
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)
I have double-checked my eclipse build path configuration for the project and it also has junit-dep and no junit and hamcrest 1.2. Furthermore, there are no compile errors, so this is just a runtime thing. Does eclipse have a runtime of it's own that's interfering here? How can I work around this?
The code that is causing the exception is here:
private NamespaceContext namespaceContext = new MetaNamespaceContext();
@Test
public void testGetPhenomenonGroupsXml() throws Exception {
WebClient webClient = new WebClient();
webClient.addRequestHeader("Accept", "application/xml");
XmlPage xmlResult = webClient.getPage(BASE_URL);
//printDoc(xmlResult.getXmlDocument(), System.out);
assertThat("count of groups",
xmlResult.getXmlDocument(),
hasXPath("count(/phenomenonGroups/om:phenomenonGroup)",
namespaceContext, equalTo("4")));
assertThat("first group",
xmlResult.getXmlDocument(),
hasXPath(
"/phenomenonGroups/om:phenomenonGroup/om:quickYesNoPhenomenon/id/text()",
namespaceContext, equalTo("1")));
}
I'm not sure if that's helpful without a ton of context - i.e. the web service code, the full class code, dependent classes like MetaNamespaceContext, etc. However, I am confident that since this all works when I do a mvn clean install
from the command line, it must be an eclipse configuration issue and have little to do with the specific code being run. (Except, of course, that the hasXPath(String, NamespaceContext, Matcher)
method is only available in 1.2 and everything would work fine if I just used hasXPath(String, Matcher)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过配置构建路径并将 hacrest 1.2 库移动到订单顶部来修复此问题。当然,如果我再次执行
mvn eclipse:eclipse
,我必须再次修复它,所以这不是一个非常好的解决方案,但它现在可以工作。I fixed this by configuring my build path and moving the hacrest 1.2 libraries to the top of the order. Of course, if I ever do a
mvn eclipse:eclipse
again, I have to go in and fix it again, so it's not a terribly nice solution, but it works for now.按 Ctrl+Shift+T 打开类型搜索框。输入 org.hamcrest.Matchers
这将显示项目类路径上包含此类的所有位置。您应该在“匹配项目:”下看到多个条目,并且您会发现它们是不同版本的 hamcrest。
Press Ctrl+Shift+T to bring up the type search box. type org.hamcrest.Matchers
This will show you all the locations on the project classpath that contains this class. You should see more than one entry under 'Matching items:', and you will find that they are different versions of hamcrest.