gpt4 book ai didi

junit - 在 Eclipse RCP 项目中运行普通 JUnit + Mockito 时出现 SecurityException

转载 作者:行者123 更新时间:2023-12-02 13:45:08 25 4
gpt4 key购买 nike

我有一个带有多个插件的 Eclipse RCP 项目。我正在编写普通的 JUnit 测试(不依赖于 Eclipse/UI)作为测试插件的单独片段。

当使用 Mockito 并尝试从另一个插件模拟接口(interface)(正确导出;我可以在代码中使用该接口(interface))时,我收到与类签名相关的 SecurityException:

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface ch.sbb.polar.client.communication.inf.service.IUserService
Mockito can only mock visible & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)

[...]

Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)

[...]

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

[...]

Caused by: java.lang.SecurityException: Signers of 'ch.sbb.polar.client.communication.inf.service.IUserService$$EnhancerByMockitoWithCGLIB$$a8bfe723' do not match signers of other classes in package

at java.lang.ClassLoader.checkPackageSigners(ClassLoader.java:361)

at java.lang.ClassLoader.defineClass(ClassLoader.java:295)

... 40 more

当我将测试作为“JUnit 插件测试”运行时,即在 OSGi 环境中,一切都按预期运行。但由于速度原因,我想使用普通的 JUnit 执行;在被测试的类中,我不需要OSGi环境。

有人知道如何做到这一点吗?

最佳答案

正如评论中提到的,根本原因是 Mockito 的 Eclipse Orbit 包(我已将其添加到我的目标平台)已签名,并且由于底层 CGLIB 中的错误,您无法模拟未签名的类/与签名的 Mockito 接口(interface)。

参见https://code.google.com/p/mockito/issues/detail?id=393以获得最详细的描述。该错误已在 CGLIB head 中修复。 ,但有not yet appeared in a release 。 Mockito 仅使用已发布的版本作为依赖项,因此 Mockito 尚未修复该问题,(对我来说)时间表未知,具体时间未知。

解决方法:在单独的 bundle 中提供未签名的 Mockito

解决方法是将 Mockito JAR(及其依赖项)打包在自己的包中,并导出必要的 API 包。

当使用 Maven Tycho、JUnit、Hamcrest 和 Mockito 时,我能够正确完成这项工作并解决所有依赖项/类路径/类加载器问题的唯一方法是以下方式:

  • 使用 pom.xml 中的以下条目创建 Maven 模块:

    <packaging>eclipse-plugin</packaging>

    [...]

    <dependencies>
    <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.19</version>
    </dependency>
    </dependencies>

    [...]

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
    <execution>
    <id>copy-test-libs</id>
    <goals>
    <goal>copy-dependencies</goal>
    </goals>
    <configuration>
    <outputDirectory>lib</outputDirectory>
    <stripVersion>true</stripVersion>
    <includeScope>runtime</includeScope>
    </configuration>
    </execution>
    </executions>
    </plugin>
  • 在 MANIFEST.MF 中使用以下条目:

    Bundle-ClassPath: lib/mockito-core.jar,
    lib/objenesis.jar
    Export-Package: org.mockito,
    org.mockito.runners
    Require-Bundle: org.junit;bundle-version="4.11.0";visibility:=reexport,
    org.hamcrest.library;bundle-version="1.3.0";visibility:=reexport,
    org.hamcrest.core;bundle-version="1.3.0";visibility:=reexport
  • 最后在单元测试片段中,添加这个新包作为依赖项。

关于junit - 在 Eclipse RCP 项目中运行普通 JUnit + Mockito 时出现 SecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com