1

hamcrest ライブラリ (1.2) を使用する単体テストがあります。hasXPath マッチャーに名前空間コンテキストを含めたいので、1.2 であることが重要です。これは Maven プロジェクトであり、すべての依存関係が正しく機能するように設定されています。(私は junit ではなく junit-dep のみを使用していることを確認します - 面倒ですが、依存関係ツリーが正しいことを確認しました。) 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もありません。さらに、コンパイル エラーがないため、これは単なる実行時の問題です。日食には、ここで干渉している独自のランタイムがありますか? どうすればこれを回避できますか?

例外の原因となっているコードは次のとおりです。

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

4

2 に答える 2

2

ビルド パスを構成し、hacrest 1.2 ライブラリを順序の一番上に移動することで、これを修正しました。もちろん、もう一度やり直した場合は、mvn eclipse:eclipseもう一度修正する必要があるため、これはあまり良い解決策ではありませんが、今のところは機能しています。

于 2010-11-23T19:24:27.380 に答える
0

Ctrl+Shift+T を押して、タイプ検索ボックスを表示します。org.hamcrest.Matchers と入力します

これにより、このクラスを含むプロジェクト クラスパス上のすべての場所が表示されます。「Matching items:」の下に複数のエントリが表示され、それらが異なるバージョンの hamcrest であることがわかります。

于 2014-12-14T21:07:29.373 に答える