20

Trying to run instrumentation test on AS.

stuck with this Error:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66) at java.lang.reflect.Proxy.invoke(Proxy.java:393) at $Proxy4.isTypeMockable(Unknown Source)

ExampleInstrumentedTest.java

      @RunWith(AndroidJUnit4.class)
        public class ExampleInstrumentedTest {
        
            @Mock
            Context context;
     
  @Before
    public void init(){
        MockitoAnnotations.initMocks(this);
    }

        @Test
            public void testDisabledFlag()  {
                ChanceValidator chanceValidator  = new ChanceValidator(context);
                Validator.ValidationResult result = chanceValidator.validate(2);
                assertEquals(result, Validator.ValidationResult.NO_ERROR);
        }
       }

build.gradle
apply plugin: 'com.android.application'

     android{
        ..
        defaultConfig {
                testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        
         testOptions {
                unitTests.returnDefaultValues = true
            }
    }
    
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        // Unit testing dependencies
        testCompile 'junit:junit:4.12'
        // Set this dependency if you want to use the Hamcrest matcher library
        testCompile 'org.hamcrest:hamcrest-library:1.3'
        // more stuff, e.g., Mockito
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.1.0'
        compile project(':mortar')
        compile project(':mockito-core-2.6.6')
    }
        

Update: After commenting line-

MockitoAnnotations.initMocks(this);

It is building fine(No Exception) but context mocked is now null.

AskQ
  • 4,215
  • 7
  • 34
  • 61

6 Answers6

19

This Worked in my case:

dependencies { 
def mockito_version = '2.7.1' // For local unit tests on your development machine
 testCompile "org.mockito:mockito-core:$mockito_version" // For instrumentation tests on Android devices and emulators
 androidTestCompile "org.mockito:mockito-android:$mockito_version"
 }

I didn’t comment initMocks

AskQ
  • 4,215
  • 7
  • 34
  • 61
  • 30
    Still getting **java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker** – IgorGanapolsky Mar 07 '17 at 17:07
  • 4
    What is important here is that `testCompile` needs the `mockito-core` artifact and `androidTestCompile` needs the `mockito-android` artifact. My problem was that I used `mockito-android` for both which led to the error. – Wim Deblauwe Oct 22 '19 at 12:01
5

In my case, I was working on a project that does not use the maven build system. So this is what worked for me.

Navigated to the maven repo for mockito (used v2.26): https://mvnrepository.com/artifact/org.mockito/mockito-core/2.26.0. I downloaded the jar. On the same page at the bottom, I looked up the dependencies. For mockito 2.26.0, these dependencies are:

In Eclipse I created a user library containing the four jar file and added it to my project.

NB: (creating the library is optional, you can add the jars directly to your project build path)

Hope this helps someone.

Skelli
  • 283
  • 2
  • 10
  • This doesn't work. Mockito in its MANIFEST.MF file has a list of Buddy-/Objenesis-dependencies with explicit versions to use. – chatlanin Feb 27 '20 at 10:49
  • I've tried to use and replace with last versions (1.10.8 \ 3.0.1) of these libs, but at tests launching Studio forcibly loads (1.10.5 \ 2.6 ) – chatlanin Feb 27 '20 at 11:35
3

Do not explicitly include mockito, let powermock pull in what it needs.

TimP
  • 925
  • 1
  • 8
  • 13
  • I also have the same problem. I added the dependencies for the mockito-core but not for mockito-android, because I am not running the tests in Android device. I run tests on Linux environment only. `testCompile "org.mockito:mockito-core:2.8.47"` – MMJ Aug 02 '17 at 21:11
  • Nice shot!!!!!! – Instein Mar 22 '22 at 21:32
1

I got this problem resolved after adding transitive dependencies for 'mockito-core'. I was facing this problem in eclipse. I was using 'mockito-core 3.8.0' along with 'mockito-junit-jupiter 3.8.0'. At first I tried to resolve this by changing JRE to JDK in Project/ Java Build Path ((as many have posted this as resolution), but that did not solve the problem. Then I added below 3 transitive dependencies for 'mockito-core 3.8.0' explicitly, and it worked!

1. net.bytebuddy » byte-buddy  v1.10.20
2. net.bytebuddy » byte-buddy-agent  v1.10.20
3. org.objenesis » objenesis  v3.1

(https://mvnrepository.com/artifact/org.mockito/mockito-core/3.8.0 - see compiled dependencies)

Ajay Singh
  • 441
  • 6
  • 18
0

I am using Quarkus on a big project with many people.

Most of our microservices used this dependency version

<net.bytebuddy.version>1.12.9</net.bytebuddy.version>

One microservice used:

<net.bytebuddy.version>1.11.0</net.bytebuddy.version>

Which was not compatible with our

<artifactId>quarkus-junit5-mockito</artifactId>

When I added more tests on a resource, I got the error of this question.

I changed the bytebuddy to 1.12.9 and mockito worked.

Make sure your bytebyddy's version is compatible with you mockito version.

Updated either one of them to be compatible with each other.

0

Try using latest version of junit. With junit version 4.2 & no byte-buddy jar I was getting this issue. Using junit 4.13.2 helped me fix this issue.

Siddhartha
  • 492
  • 1
  • 5
  • 15