8

When i add my mockito library to class path, and use a simple mockito example for testing where i try to return a wrong value for the function add by using the mock object, i get java.lang.verifyerror. Following is the code used for testing followed by logcat.

    @Test
    public void testadd()
    {
        maincode obj2=mock(maincode.class);
        when(obj2.add(0, 0)).thenReturn(9);
        assertEquals(obj2.add(0, 0),9); 
    }

I get the following error. Please help! thx.

java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105) at org.mockito.cglib.proxy.Enhancer.(Enhancer.java:70) at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56) at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51) at org.mockito.Mockito.mock(Mockito.java:1243) at org.mockito.Mockito.mock(Mockito.java:1120) at testaddmock.testadd(testaddmock.java:24) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
user3054298
  • 123
  • 2
  • 7
  • this error comes due to jar issues. Are you having the correct jars in your classpath see http://stackoverflow.com/questions/100107/reasons-of-getting-a-java-lang-verifyerror – Dev Blanked Dec 03 '13 at 13:09
  • I looked at the linked question. In my case, I have only one "mockito" library on my machine (mockito-all-1.9.5.jar) so I don't see how there could be a runtime mismatch. – Brian White Dec 31 '13 at 23:54
  • Maybe you have any other jar around that contains mockito classes? Try printing out the classpath (http://www.mkyong.com/java/how-to-print-out-the-current-project-classpath/) and append it to the question. – Bernhard Jan 02 '14 at 11:31

4 Answers4

13

By default, Mockito uses cglib to create dynamic proxies, but for Android this will not work, because cglib generates .class files, not .dex. But starting from version 1.9.5 Mockito offers an extension point that allows replacing the proxy generation engine, so all you need is change this engine and I think Dexmaker is the best variant.

So to make it works you should just add dexmaker-mockito-1.0.jar to your project, and Mockito will use it to generate his proxies.

Grimmy
  • 2,041
  • 1
  • 17
  • 26
8

@Grimmys answer (+1) has the correct answer for me.

When running on Android adding the missing gradle imports for Dexmaker is all thats required

androidTestCompile 'org.mockito:mockito-core:1.9.5' 
androidTestCompile 'com.google.dexmaker:dexmaker:1.1' 
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
Community
  • 1
  • 1
Dori
  • 18,283
  • 17
  • 74
  • 116
2

@Grimmy dexmaker-1.0.jar is also needs.

@user3054298 I met the same problem & solved by following this blog's guide:http://corner.squareup.com/2012/10/mockito-android.html

A new feature in Mockito 1.9.5 is support for Android’s Dalvik runtime. It uses DexMaker to generate mock classes on the fly. To use Mockito on a device or emulator, you’ll need to add three .jar files to your test project’s libs directory: mockito-all-1.9.5.jar, dexmaker-1.0.jar, and dexmaker-mockito-1.0.jar.

Spec
  • 415
  • 1
  • 5
  • 10
0

I was stuck on a similar error for 3 days! Adding dexmaker libraries didn't help at all! In the end I changed the version of power mock to the latest 1.5.6 and it worked fine after that! Hope this helps someone as well.

sasfour
  • 399
  • 1
  • 3
  • 10