相关文章推荐
仗义的铅笔  ·  Flutter:flutter_local_ ...·  2 月前    · 
风流的松树  ·  How To Configure Java ...·  1 年前    · 
冲动的凉茶  ·  python jieba.cut ...·  1 年前    · 
慷慨大方的西装  ·  PYCHARM下用OPENCV ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

Do yourself a favor, and use a buid tool that handles dependencies (i.e.gradle or Maven, as explained in the documentation (site.mockito.org). Here are the direct dependencies it would automatically get for you if you used one: site.mockito.org. Of course those dependencies can themselves have other dependencies, etc. etc. Which is one of the reasons we use such build tools. – JB Nizet Oct 5, 2018 at 19:10 @JBNizet I am working on an offline system that will never be connected to the internet. The other internet connected network I use is locked down such that I cannot create a local repository from which to package dependencies. Apart from that, I would love to use that route as it would save me so much hassle. – MT0 Oct 5, 2018 at 19:23 I'm talking about a build tool. The build tool is not used at runtime. You use it to download the dependencies and buid your app. The app can then be deployed anywhere you want, and doesn't need any internet access. Not to mention that Mockito is typically used to test your code. It isn't used at runtime. – JB Nizet Oct 5, 2018 at 19:25

@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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.