JMOCK 依赖问题

发布于 2024-10-12 19:49:51 字数 4517 浏览 0 评论 0原文

我正在尝试完成我的第一个 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 中的 hamcrest jar 而不是

Junit 中的 hamcrest jar?我说得对吗?我应该在 Eclipse 中做什么来实现它?

谢谢,

莎拉

I am trying to get through my very first JMOCK tutorial http://www.jmock.org/getting-started.html, and it didn't go well.

The problem I encountered is below:


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)

I've found a solution on the internet. Please see below:

The solution is to ensure that any dependencies on JMock JARs occur
before dependencies on JUnit in every plug-in. That way, Hamcrest is
loaded from JMock, not from JUnit.

My understanding of the solution is: make the test class use the hamcrest jar from JMock instead of the one from Junit? Am I right? What should I do in Eclipse to make it happen?

Thanks,

Sarah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷心人i 2024-10-19 19:49:51

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

hamcrest-core-1.2.jar
hamcrest-library-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构建路径属性(配置构建路径)中的“Order and Export”选项卡,您会发现JUnit jar位于hamcrest.jar之上。您可以将 hamcrest 移至此处的 JUnit jar 上方,问题就会消失。

The libraries order in the Eclipse build configuration are:

hamcrest-core-1.2.jar
hamcrest-library-1.2.jar
jmock-2.5.1.jar
JRE [JavaSE-1.6]
JUnit_4.8.1.jar (part of the eclipse distribution)
hamcrest.core_1.1.0 (bundled with JUnit 4.8.1)

The solution is simple - make sure that hamcrest.jar is before the JUnit library included by Eclipse in the classpath.

I believe if you look at the "Order and Export" tab in the java build path property (Configure Build Path), you will find that the JUnit jar is above the hamcrest.jar. You can move hamcrest above the JUnit jar here and the problem will go away.

吃素的狼 2024-10-19 19:49:51

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

You could use junit-dep.jar (rather than junit.jar) which doesn't include the hamcrest types. Then the hamcrest references in jmock won't clash.

夜声 2024-10-19 19:49:51
    <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>
    <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>
反目相谮 2024-10-19 19:49:51

由于项目上重复的 JUnit 依赖关系,我发生了这种情况。一项由 eclipse 添加,一项由 Maven 依赖项添加(m2eclipse/m2e 也将这一项添加到类路径中)。

因此,通过转到“项目”>“删除由 eclipse 添加到项目的项目”属性>构建路径

见下文。
在此处输入图像描述

This happened to me due to duplicate JUnit dependencies on the project. One added by eclipse and one from Maven dependencies (m2eclipse/m2e adds this one to classpath as well).

So remove the one added by eclipse to project by going to Project > Properties > Build Path

See below.
enter image description here

许你一世情深 2024-10-19 19:49:51

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

所以我只是将 .classpath 中的“JUNIT_CONTAINER/4”更改为“JUNIT_CONTAINER/3”...并且所有测试都成功了。

I just ran into the same problem trying to run the tests in a non-Eclipse project I had just imported. After looking at the other answers here, I noticed that the pom.xml specified JUnit 3.

So I simply changed "JUNIT_CONTAINER/4" to "JUNIT_CONTAINER/3" in .classpath... and all the tests succeeded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文