Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VerifyError using Mockito 1.9.5 and DexMaker-Mockito-1.0

Like many others I was excited to hear that Mockito now works with Android and followed this tutorial to see it with my own eyes. Everything seemed fan-flapping-tastic and I got underway incorporating the mocking solution into my Android Test Project...

The error

However, on setting up my application's test project to leverage the mockito-all-1.9.5, dexmaker-1.0 and dexmaker-mockito-1.0 jars I encountered a problem with my very first test case. Precisely this problem in fact. The part that I would like assistance on is;

Caused by: 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.<clinit>(Enhancer.java:70)

I have been informed that this "simply doesn't quite work yet" since the stack trace implies that the DexMaker jar is not being used - reference this response. However, I am suspicious that I am doing something wrong with respect to my project set-up so I'm looking to draw from the collective knowledge base here to see if indeed this is user error or a beta-bug.

My Android Test Project set-up

Please find below a screenshot of my test project's configuration. The project was created via the Android Wizard and shares no special features other than the inclusion of the Mockito and DexMaker jars (mentioned above) under the libs directory.

Test Project Configuration

The Test

Never mind the content of the test (the test fails before the unit test is executed) the set-up is as described below;

public class TestSpotRatingCalculator extends InstrumentationTestCase {
  @Mock
  private AService aService; // Changed the service names being used here - not important.
  @Mock
  private BService bService;
  @Mock
  private CService cService;
  @Mock
  private DService dService;

  /**
   * @see android.test.AndroidTestCase#setUp()
   */
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    MockitoAnnotations.initMocks(this);  // Failure here with aforementioned stacktrace...
  }

If anyone out there has an idea what is wrong then please sound-off here.

like image 988
BrantApps Avatar asked Oct 24 '12 12:10

BrantApps


2 Answers

Hi I had the same problem and I found this article really usefull!

http://corner.squareup.com/2012/10/mockito-android.html

The key piece of information is:

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.

like image 141
Shilaghae Avatar answered Sep 24 '22 11:09

Shilaghae


Just add this in your gradle:

androidTestCompile 'org.mockito:mockito-core:1.10.8'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
like image 28
user1007522 Avatar answered Sep 23 '22 11:09

user1007522