Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Mockito with dexmaker for Android Testing?

I am trying to get Mockito (1.9.5 rc1) working in my Android tests (using Robotium). For making it run on the DVM I put dexmaker.jar and the dexmaker-mockito.jar (v 0.9) in the classpath of the test-project.

When run following test as an Android JUnit test in Eclipse:

package com.stampay.pos.test;

import roboguice.RoboGuice;
import android.app.Application;

import com.jayway.android.robotium.solo.Solo;
import com.stampay.pos.activities.HomeActivity;
import com.stampay.pos.model.Consumer;
import com.stampay.pos.util.ScannerAndroid;

import static org.mockito.Mockito.*;

public class HomeActivityTest extends ActivityTest<HomeActivity> {
    private Solo solo;
    Application app;

    public HomeActivityTest() {
        super(HomeActivity.class);

    }

    @Override
    public void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
        RoboGuice.util.reset();
    }

    public void testScan() throws Exception {

        ScannerAndroid scanner = (ScannerAndroid) getActivity().getScannerHelper();

        Consumer mockConsumer = mock(Consumer.class);

        //scanner.generateResult("consumer1");
    }
}

I get the following stack trace:

java.lang.ExceptionInInitializerError
at com.stampay.pos.test.HomeActivityTest.testScan(HomeActivityTest.java:41)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Caused by: java.lang.ExceptionInInitializerError
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:932)
... 15 more
Caused by: java.lang.ExceptionInInitializerError
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
... 17 more
Caused by: org.mockito.exceptions.misusing.MockitoConfigurationException: Failed to load interface org.mockito.plugins.MockMaker using jar:file:/data/app/com.stampay.pos.test-2.apk!/mockito-extensions/org.mockito.plugins.MockMaker
... 15 more
Caused by: java.lang.ClassNotFoundException: com.google.dexmaker.mockito.DexmakerMockMaker
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:133)
at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:102)
at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:61)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:932)
... 15 more

Any suggestions? http://code.google.com/p/dexmaker/ and http://code.google.com/p/mockito/wiki/ReleaseNotes do not provide any helpful information.

A similar problem seems to be reported here: http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/#comment-65955

UPDATE: with the .jar files from https://github.com/paulbutcher/mockito-on-android/tree/master/WarehouseManagerTest/libs all works smoothly, though he uses an outdated snapshot of Mockito based on 1.9.1

like image 329
Benjamin Avatar asked Sep 27 '12 16:09

Benjamin


1 Answers

I encountered the same problem attempting to use mockito 1.9.5-rc1.

The dexmaker page notes that you need 1.9.5+ for this, and apparently RC1 doesn't cut it. Mockito 1.9.5 appears to have just been released days ago, so it should show up shortly in Maven Central.

In the meantime, you can download the 1.9.5 zip from Mockito's site. If you're using Maven, you could manually install those jars to your local repo with mvn install:install-file. I also got this to work by checking out the latest Mockito source (unreleased 1.9.8), building, and installing to my local Maven repo.

like image 153
RonU Avatar answered Oct 05 '22 23:10

RonU