Export to GitHub

mockito - issue #57

Using Mockito with Android virtual machine


Posted on Feb 15, 2009 by Helpful Hippo

I get the following exception trying to create a mock inside the Android Dalvik JVM. I'm using mockito-all-1.7 (though the same excepiton comes from mockito-all-1.6). the class I'm mocking does not extend any Android class or implement any Android interface.

The problem can be recreated by trying to instantiate a mock when the test is run using the Android SDK utility "adb shell am instrument ..."

java.lang.ExceptionInInitializerError at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:75) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:57) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:27) at org.mockito.Mockito.mock(Mockito.java:561) at org.mockito.Mockito.mock(Mockito.java:514) at com.wilson.android.library.BookServiceFetchingHandlerTest.testHandleMessage_ShowsDialog_ForException(BookServiceFetchingHandlerTest.java:28) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:155) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:142) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:313) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1508) Caused by: java.lang.VerifyError: org.mockito.cglib.core.ReflectUtils at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:166) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216) at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:116) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:108) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:104) at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69) ... 18 more

Comment #1

Posted on Feb 15, 2009 by Massive Monkey

Hi, can you attach a zip with all required jars & the failing example?

Comment #2

Posted on Feb 17, 2009 by Helpful Hippo

I can try to put something together. It's going to be involved. The Android SDK includes the SDK - no Android phone is required.

Comment #3

Posted on Feb 19, 2009 by Massive Monkey

Ok, I'm downloading the SDK and will try to reproduce it.

Comment #4

Posted on Feb 19, 2009 by Helpful Hippo

Here is a tar of an eclipse project that you can sync to the emulator. You should be able to import this project into an eclipse installation that has the android plugin. Then run "run as... Android Application" will push the files to the emulator. once that's done, the adbtest.sh script will execute the test inside the emulator, sending the error to stdout.

Attachments

Comment #5

Posted on Feb 22, 2009 by Massive Monkey

Thanks, we'll look at it. From the way you run it I guess it's executed on different than usual jvm. This might be the problem.

Comment #6

Posted on Feb 22, 2009 by Helpful Hippo

That's right, it is a different JVM named Dalvik.

Comment #7

Posted on Feb 26, 2009 by Massive Monkey

I can't get through the last step. When I execute the script here is what happens:

C:\projects\debugging\ExampleAndroidTest>adb shell am instrument -w -e class exa mple.android.test.BarTest example.android.test/android.test.InstrumentationTestR unner INSTRUMENTATION_FAILED: example.android.test/android.test.InstrumentationTestRun ner usage: am [start|broadcast|instrument] am start -D INTENT am broadcast INTENT am instrument [-r] [-e ] [-p ] [-w]

   INTENT is described with:
            [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
            [-c <CATEGORY> [-c <CATEGORY>] ...]
            [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
            [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
            [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
            [-n <COMPONENT>] [-f <FLAGS>] [<URI>]

Comment #8

Posted on Feb 27, 2009 by Helpful Hippo

it looks like the usage error is a red herring. I uninstalled the package from my Emulator and got the same error. So my guess to your problem is that you still need to install the project to the emulator. If you right click on the project and choose "Run As -> Android Application" you should see a series of log messages in the Android Console ending in the following: [2009-02-26 21:21:40 - ExampleAndroidTest] Installing ExampleAndroidTest.apk... [2009-02-26 21:21:44 - ExampleAndroidTest] Success! [2009-02-26 21:21:44 - ExampleAndroidTest] /ExampleAndroidTest/bin/ExampleAndroidTest.apk installed on device [2009-02-26 21:21:44 - ExampleAndroidTest] Done!

Comment #9

Posted on Feb 27, 2009 by Massive Monkey

Thanks for helping out with getting this issue reproduced!

"Run As -> Android Application"

Yes, I got it working -> it's all good I can see "[2009-02-27 09:53:26 - ExampleAndroidTest] Done!" message. I have the emulator working in eclipse so I start up the script in the command line. It fails with the error - see error log in my previous entry - INSTRUMENTATION_FAILED stuff.

Comment #10

Posted on Feb 28, 2009 by Helpful Hippo

We can try some basic stuff to sniff out the problem:

1) run "adb devices" - the output should be your running emulator. It's usually: emulator-5554 device 2) run "adb shell pm list packages" - make sure the output contains the package package:example.android.test

Comment #11

Posted on Feb 28, 2009 by Massive Monkey

I cannot work on it at the moment - I'm off for holidays for about a month. I asked a colleague to look at it but I don't when he does it. You might grab the sources and try to fix it if you feel like :)

Comment #12

Posted on Apr 21, 2009 by Massive Monkey

This won't go the next release - we don't even know if it is fixable because it is a runtime problem on different kind of virtual machine. Will get back to it once we have time.

If there's a developer who knows android enough and is not afraid of some debugging sessions with Mockito - go kill this bug :)

Comment #13

Posted on May 2, 2009 by Massive Monkey

(No comment was entered for this change.)

Comment #14

Posted on May 7, 2009 by Helpful Hippo

I have a hunch that this is not Mockito related. I hope to have some more time to finish tracing it down.

Comment #15

Posted on May 7, 2009 by Massive Monkey

It would be great. Thank you!

Comment #16

Posted on Sep 22, 2009 by Massive Monkey

I'm closing this one as apparently no one has time for it. Most likely it's an issue with different vm.

Comment #17

Posted on Oct 24, 2009 by Helpful Cat

There is useful information about mocking on Dalvik from some Googlers here - https://sites.google.com/site/androiddevtesting/

Status: WontFix

Labels:
Type-Defect Priority-Low