Unable to initialize Mockito

只谈情不闲聊 提交于 2021-02-10 16:50:35

问题


I am trying to set up Mockito 2.22 and have downloaded mockito-core-2.22.2.jar and byte-buddy-1.9.0.jar and, in the Netbeans 8.2 project, I have added those two jar files, JUnit 4.12 and Hamcrest 1.3 to the test libraries.

When I try to run the MCVE:

package com.stackoverflow.test;

import org.junit.Test;
import static org.mockito.Mockito.mock;

public class SimpleMockTest {
    public static class A{
        public String value(){ return "A"; }
    }
    @Test
    public void testASimpleMock()
    {
        A mocked = mock( A.class );
    }
}

When I try to run the test, I get the error:

Could not initialize plugin: interface org.mockito.plugins.InstantiatorProvider2 (alternate: interface org.mockito.plugins.InstantiatorProvider)
java.lang.IllegalStateException
    at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
    at com.sun.proxy.$Proxy12.getInstantiator(Unknown Source)
    at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:44)
    at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:69)
    at org.mockito.Mockito.mock(Mockito.java:1895)
    at org.mockito.Mockito.mock(Mockito.java:1804)
    at com.stackoverflow.test.SimpleMockTest.testASimpleMock(SimpleMockTest.java:13)
    at org.mockito.internal.creation.instance.ObjenesisInstantiator.<init>(ObjenesisInstantiator.java:16)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:66)
    at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.getDefaultPlugin(DefaultMockitoPlugins.java:43)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:67)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:32)
    at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
    at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:238)
    at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:68)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Is there another dependency that I am missing? Or some other configuration/annotiation/setup that needs to be done to fix the error and allow the object to be mocked?


回答1:


@JBNizet's suggestion to use Maven led me to Mockito's maven pom which lists the dependencies as:

  • byte-buddy 1.9.0
  • byte-buddy-agent 1.9.0
  • objenesis 2.6

Downloading objenesis and adding it to the project fixed the issue.



来源:https://stackoverflow.com/questions/52671804/unable-to-initialize-mockito

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!