使用OpenJDK 12使用Mockito启动JUnit时,如何摆脱“无法初始化插件:接口org.mockito.plugins.MockMaker”

时间:2019-04-03 21:45:33

标签: java unit-testing junit mockito

我一直在将一个项目从Java 8迁移到Java12。除单元测试外,其他一切都很顺利。当我使用Maven编译和启动测试时,许多测试失败,并显示以下消息:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
    at com.tetratech.csoft.ui.jfx.AppContextTest.<init>(AppContextTest.java:22)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@35fd987b
Caused by: org.mockito.exceptions.base.MockitoInitializationException: 

Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)

Java               : 12
JVM vendor name    : Oracle Corporation
JVM vendor version : 12+33
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 12+33
JVM info           : mixed mode, sharing
OS name            : Windows 10
OS version         : 10.0

Caused by: java.lang.IllegalStateException: Could not self-attach to current VM using external process

从IntelliJ启动时,我得到了更详细的消息:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)

    at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
    at com.sun.proxy.$Proxy7.isTypeMockable(Unknown Source)
    at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
    at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
    at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:240)
    at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:228)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
    at org.mockito.Mockito.mock(Mockito.java:1907)
    at org.mockito.Mockito.mock(Mockito.java:1816)
    at com.tetratech.csoft.ui.jfx.AppContextTest.<init>(AppContextTest.java:22)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@460d0a57
    at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:22)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:19)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
    ... 29 more
Caused by: org.mockito.exceptions.base.MockitoInitializationException: 
Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)

Java               : 12
JVM vendor name    : Oracle Corporation
JVM vendor version : 12+33
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 12+33
JVM info           : mixed mode, sharing
OS name            : Windows 10
OS version         : 10.0

为解决此问题,如对类似问题的解答所建议的那样,我修改了pom.xml使其包括以下依赖性:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.25.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.25.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.9.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy-agent</artifactId>
            <version>1.9.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.objenesis</groupId>
            <artifactId>objenesis</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

根据某些人的说法,这是使用JRE(而非JDK)启动测试时发生的问题。由于我使用的是OpenJDK 12,所以不是这种情况。

在Java 8中成功运行的OpenJDK 12中的测试失败,除了我向您展示的内容以外,没有其他修改。在我们的测试中,org.mockito.plugins.MockMaker是必需的,因为许多类来自于遗留代码,并且具有许多最终类。正如您在消息日志中看到的那样,测试是使用Windows 10上运行的OpenJDK 12启动的。

是否可以使用Mockito运行这些测试?我知道PowerMockito可以解决此问题,但是使用它意味着在测试类中进行了大量修改。

谢谢。

1 个答案:

答案 0 :(得分:0)

我终于能够解决我的问题。答案的关键是线

java.lang.IllegalStateException: Could not self-attach to current VM using external process

对于Java 9和更高版本,必须添加-Djdk.attach.allowAttachSelf=true作为VM参数以避免这种异常。当我这样做时,单元测试运行得很好。

这是我找到的链接:

https://github.com/raphw/byte-buddy/issues/612

https://github.com/mockk/mockk/issues/254