【发布时间】:2015-02-18 09:28:07
【问题描述】:
我有一个带有多个插件的 Eclipse RCP 项目。我正在编写普通的 JUnit 测试(不依赖于 Eclipse/UI)作为被测插件的单独片段。
当使用 Mockito 并尝试从另一个插件(正确导出;我可以在我的代码中使用该接口)模拟接口时,我得到一个与类签名相关的 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 版本,您可能会遇到这个bug。
-
@Rüdiger 是的,我认为你是对的。我在研究过程中也发现了这个错误。我现在正在尝试一种解决方法,如果可行,我将作为答案发布。但我仍然为其他人提供的任何可行的解决方案感到高兴。
-
您是否检查过是否有包含固定 CGLIB 的 Mockito 新版本?
-
好像还没有新版本。我读过 Mockito 只使用官方的 cglib 版本,而据称修复的版本还没有正式发布(尽管修复已被拉入主存储库)
标签: junit eclipse-plugin eclipse-rcp mockito osgi-fragment