我有一个使用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)
.