MockitoException: Failed to mock class com.example.example.test.MainActivity

713 views
Skip to first unread message

Phlip Plumlee

unread,
Oct 16, 2013, 6:21:57 PM10/16/13
to moc...@googlegroups.com
This SO entry will earn me another "tumbleweed" badge:

I get that exception on the first call to mock(MainActivity.class):

org.mockito.exceptions.base.MockitoException: Failed to mock class com.example.example.test.MockActivity

at com.smartphins.example.test.SmartPhinsTest.test_mockito_mocks_activities(ExampleTest.java:39)

at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
The versions are all current, and the test code itself is straight out of you guys's blogs. A little help?

Eric Lefevre-Ardant

unread,
Oct 17, 2013, 3:49:25 AM10/17/13
to moc...@googlegroups.com
I am not familiar with using Mockito in the context of Android development (and, besides, I am not a maintainer of Mockito).
That said, it'd help if you posted code snippets (test and MockActivity). I know you mention it's trivial and coming from a blog... but I don't know which one.



--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.

Brice Dutheil

unread,
Oct 17, 2013, 4:16:07 AM10/17/13
to moc...@googlegroups.com
Hi Phlip,

Eric is right, without code snippet and complete stacktrace it would be difficult to point you the right direction ;)
Also I don't think you'll find any Android code on the blogs of any Mockito commiters, as we are not really Android guys ;)


Cheers,
-- Brice

Phlip Plumlee

unread,
Oct 17, 2013, 11:00:18 AM10/17/13
to moc...@googlegroups.com
The example code is 'mock(MainActivity.class)'.

Really; the steps to reproduce are A> install Eclipse Kepler, B> generate an Android 18 app, C> fling dexmaker-1.0.jar, dexmaker-mockito-1.0.jar, and mockito-all-1.9.5.jar into its libs folder, D> write an AndroidUnitTestCase, and E> mock the activity:

package com.example.smartphinphone.test;

import android.content.Context;
import android.content.Intent;
import android.test.ActivityUnitTestCase;

import com.example.smartphinphone.MainActivity;

import static org.mockito.Mockito.*;

public class ActivityTests extends ActivityUnitTestCase<MainActivity> {

    public ActivityTests(Class<MainActivity> activityClass) {
        super(activityClass);
    }
    public ActivityTests() {
        super(MainActivity.class);
    }

    public void test_mockito_mocks_activities() {
        System.setProperty("dexmaker.dexcache", "/sdcard");
        MainActivity mockActivity = mock(MainActivity.class);
    }

}

Start a virtual device, run the tests, and that line hangs up for a long time, then goes boom.

It also goes boom if I add all the setUp() clutter required to create a real activity, then use spy(activity) on it.

The full trace has the trace I already posted, and then the usual stack of Androidy things at the bottom:

org.mockito.exceptions.base.MockitoException: Failed to mock class com.example.smartphinphone.MainActivity
at com.example.smartphinphone.test.ActivityTests.test_mockito_mocks_activities(ActivityTests.java:22)
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.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)
Caused by: java.io.IOException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:948)
at java.io.File.createTempFile(File.java:1013)
at com.google.dexmaker.DexMaker.generateAndLoad(DexMaker.java:374)
at com.google.dexmaker.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:252)
at com.google.dexmaker.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:56)
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)
... 14 more
Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at java.io.File.createNewFile(File.java:941)
... 22 more


Phlip Plumlee

unread,
Oct 17, 2013, 11:13:39 AM10/17/13
to moc...@googlegroups.com
I can debug thru Mockito down to the Dexmaker level:

DexmakerMockMaker.createMock(MockCreationSettings, MockHandler) line: 38
MockUtil.createMock(MockCreationSettings) line: 26

Then I can't see the source, so I have to go hunting for it, and installing it, before I continue. I hope installing a new Dexmaker doesn't fix the bug before I can characterize it.

Eric Lefevre-Ardant

unread,
Oct 17, 2013, 12:10:22 PM10/17/13
to moc...@googlegroups.com
I have never used Dexmaker (and it's certainly not part of Mockito), but from what I've found, it is not supposed to work on Dalvik, when used together with Mockito (which, as I gather, is what you are doing).

At least, that's what I understand from https://code.google.com/p/dexmaker/issues/detail?id=5


--

Ian Parkinson

unread,
Oct 17, 2013, 12:42:01 PM10/17/13
to moc...@googlegroups.com
Your underlying error relates to creating a file, and your DexCache is set to /sdcard. DexMaker will use the dexcache to write its generated classes. Perhaps your tests don't have write access to /sdcard? You could try getContext().getCacheDir().getPath() instead, which I think is more usual anyway. Which blog were you following?

Ian


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.



--



Google UK Limited

Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902

Phlip Plumlee

unread,
Oct 17, 2013, 2:19:55 PM10/17/13
to moc...@googlegroups.com
> getContext().getCacheDir().getPath()

        MockSettingsImpl impl = MockSettingsImpl.class.cast(settings);
        MockCreationSettings<T> creationSettings = impl.confirm(typeToMock);
        T mock = mockUtil.createMock(creationSettings);
        mockingProgress.mockingStarted(mock, typeToMock);
        return mock;

Tx. Now, as far as I can tell (this is between distractions today sry), it hangs up forever on either the createMock() line or the mockingStarted() line. Small chance that changing the SD card path moved the hangup to the second line, but I can't tell yet.

And _everyone_ can write to the SD card, right??

VRS

unread,
Oct 18, 2013, 1:08:49 PM10/18/13
to moc...@googlegroups.com
In the setup method put this:

// Some issues with dexmaker
System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());
Thread.currentThread().setContextClassLoader(
getClass().getClassLoader()); // <== maybe you also have a sharedUser...so put this.

Phlip Plumlee

unread,
Oct 18, 2013, 3:37:16 PM10/18/13
to moc...@googlegroups.com
VRS wrote:
 > Thread.currentThread().setContextClassLoader(
 > getClass().getClassLoader()); // <== maybe you also have a sharedUser...so put this.

I already tried that tx. I'm now writing a summary post on Dexmaker's github issues page.
Reply all
Reply to author
Forward
0 new messages