Question

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).

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top