JMOCK依赖问题

时间:2011-01-21 04:40:35

标签: jmock

我正在尝试通过我的第一个JMOCK教程http://www.jmock.org/getting-started.html,并且进展不顺利。

我遇到的问题如下:


java.lang.SecurityException: class "org.hamcrest.TypeSafeMatcher"'s signer information does not match signer information of other classes in the same package
    at java.lang.ClassLoader.checkCerts(Unknown Source)
    at java.lang.ClassLoader.preDefineClass(Unknown Source)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.jmock.internal.InvocationExpectationBuilder.createExpectationFrom(InvocationExpectationBuilder.java:86)
    at org.jmock.internal.InvocationToExpectationTranslator.invoke(InvocationToExpectationTranslator.java:19)
    at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
    at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
    at $Proxy8.receive(Unknown Source)
    at PublisherTest$1.(PublisherTest.java:35)
    at PublisherTest.oneSubscriberReceivesAMessage(PublisherTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:37)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:105)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:96)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:52)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:50)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    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)

我在互联网上找到了解决方案。请参阅以下内容:

解决方案是确保发生对JMock JAR的任何依赖 在每个插件中依赖于JUnit之前。那样,哈姆克雷斯特就是 从JMock加载而不是从JUnit加载。

我对解决方案的理解是:让测试类使用JMock的hamcrest jar而不是Junit的jar?我对吗?我应该在Eclipse中做些什么来实现它?

谢谢,

萨拉

5 个答案:

答案 0 :(得分:8)

Eclipse构建配置中的库顺序为:

hamcrest核-1.2.jar hamcrest库-1.2.jar JMock的-2.5.1.jar JRE [JavaSE-1.6] JUnit_4.8.1.jar(eclipse发行版的一部分) hamcrest.core_1.1.0(与JUnit 4.8.1捆绑在一起)

解决方案很简单 - 确保hamcrest.jar位于类路径中Eclipse包含的JUnit库之前。

我相信如果你看一下java build path属性(Configure Build Path)中的“Order and Export”选项卡,你会发现JUnit jar在hamcrest.jar之上。您可以在此处将Hamcrest移动到JUnit jar上方,问题就会消失。

答案 1 :(得分:2)

您可以使用不包含hamcrest类型的junit- dep .jar(而不是junit.jar)。然后jmock中的hamcrest引用不会发生冲突。

答案 2 :(得分:2)

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit-dep</artifactId>
        <version>4.8.2</version>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
        </exclusions>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3.0RC2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jmock</groupId>
        <artifactId>jmock</artifactId>
        <version>2.6.0-RC2</version>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-library</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-unit-test</artifactId>
            </exclusion>
        </exclusions>
        <scope>test</scope>
    </dependency>
    <!-- next libs are optional -->
    <dependency>
        <groupId>org.jmock</groupId>
        <artifactId>jmock-junit3</artifactId>
        <version>2.6.0-RC2</version>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jmock</groupId>
        <artifactId>jmock-legacy</artifactId>
        <version>2.6.0-RC2</version>
        <scope>test</scope>
    </dependency>

答案 3 :(得分:2)

由于项目中存在重复的JUnit依赖性,这发生在我身上。一个由eclipse添加,一个来自Maven依赖项(m2eclipse / m2e也将此类添加到类路径中)。

通过转到Project&gt;删除eclipse添加的项目。属性&gt;构建路径

见下文。 enter image description here

答案 4 :(得分:0)

我遇到了同样的问题,试图在我刚刚导入的非Eclipse项目中运行测试。看完这里的其他答案后,我注意到pom.xml指定了JUnit 3

所以我只是改变了#34; JUNIT_CONTAINER / 4&#34;到&#34; JUNIT_CONTAINER / 3&#34;在.classpath ...并且所有测试都成功了。