1

I have a well reported issue here:

Mockito + Dexmaker on Android

However the solution does not work.

I am using Robolectric and even when I try setting the dexmaker cache location:

@Before
public void setUp()
{
    System.setProperty("dexmaker.dexcache", RuntimeEnvironment.application.getCacheDir().getPath());
}

The error changes to

java.lang.RuntimeException: java.lang.NullPointerException

at com.android.dx.DexMaker.generateClassLoader(DexMaker.java:366)
at com.android.dx.DexMaker.generateAndLoad(DexMaker.java:439)
at com.android.dx.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:264)
at com.android.dx.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:56)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.spy(Mockito.java:1368)

Do you have any ideas that will enable me to create Mock or Spy objects using Robolectric?

Note: When I don't use Robolectric and use a regular ActivityInstrumentationTestCase2 and @RunWith(AndroidJUnit4.class) I can create Mock and Spy objects...

This issue has arisen after Mockito 1.9

Community
  • 1
  • 1
Aggressor
  • 13,323
  • 24
  • 103
  • 182
  • Possible duplicate of [Robolectric + Mockito](http://stackoverflow.com/questions/35970899/robolectric-mockito) – Jeff Bowman May 13 '16 at 21:40
  • My problem is different and the solution is different. – Aggressor May 13 '16 at 21:45
  • Your solution is different because Mockito didn't support dexmaker prior to 1.9.5. If you ensure you don't use mockito-android, you can use any version you'd like. – Jeff Bowman May 13 '16 at 21:46
  • @Jeff Bowman is `mockito-android` a specific SDK you are referring to? I don't have any dependency for that – Aggressor May 13 '16 at 22:13
  • It's a name I've seen used in Maven and Gradle for a Mockito package that includes dexmaker. If you can use Mockito in instrumentation tests, then you are including Dexmaker one way or another. Check your build rules, and consider updating your question to include it. – Jeff Bowman May 13 '16 at 22:17
  • 1
    You don't need dexmaker for Robolectric. Robolectric is used with jvm plain junit tests, that are run on build machine and don't require dexiing – Eugen Martynov May 14 '16 at 13:17
  • Also please remove robolectric-gradle plugin. It is outdated – Eugen Martynov May 14 '16 at 13:25
  • In fact, I'm quite sure it's the mistaken inclusion of dexmaker that's the problem. (By "include it" I meant "include your build file in your question, if you need more help".) – Jeff Bowman May 14 '16 at 16:28

2 Answers2

1

I just found this workaround:

https://comadeblog.wordpress.com/2013/11/12/dexcachenull-for-mockito-on-robolectric/comment-page-1/

TL;DR:

Use compile 'org.mockito:mockito-core:1.9.0'. Higher versions will throw this error.

This is the only way I have found to get it work with Robolectric.

Aggressor
  • 13,323
  • 24
  • 103
  • 182
1

Robolectric is used for unit tests that are run on build machine JVM. You don't need Dexmaker for these tests.

So please correct your build.gradle:

androidTestCompile "com.crittercism.dexmaker:dexmaker:${versions.dexmaker}"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:${versions.dexmaker}"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:${versions.dexmaker}"
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • It is true that I could safely remove those, but I still get the same error unless I use Mockito 1.9 or less. – Aggressor May 16 '16 at 23:38