0

This is my test class:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk=19)
public class LoginPresenterTest {

    LoginPresenter loginPresenter;

    @Mock
    private LoginView loginView;

    @Mock
    private LoginNetworkOperation loginNetworkOperation;

    @Before
    public void setUp() {
        /**
         * The following line is a fix for a popular Mockito problem
         */
        System.setProperty("dexmaker.dexcache", RuntimeEnvironment.application.getCacheDir().getPath());
        MockitoAnnotations.initMocks(this);
        loginPresenter = LoginPresenter_.getInstance_(RuntimeEnvironment.application);
    }

    // Some tests here, omitted 
}

LoginView gets mocked just fine (it is an interface) but attempting to mock LoginNetworkOperation throws this error:

Caused by: java.lang.NoSuchMethodError: java.lang.System.logW(Ljava/lang/String;)V
    at dalvik.system.DexPathList.makeDexElements(DexPathList.java:245)
    at dalvik.system.DexPathList.__constructor__(DexPathList.java:112)
    at dalvik.system.DexPathList.<init>(DexPathList.java)
    at dalvik.system.BaseDexClassLoader.__constructor__(BaseDexClassLoader.java:48)
    at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java)
    at dalvik.system.DexClassLoader.<init>(DexClassLoader.java)
    at com.google.dexmaker.DexMaker.generateAndLoad(DexMaker.java:382)
    at com.google.dexmaker.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:258)
    at com.google.dexmaker.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:55)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
    at org.mockito.Mockito.mock(Mockito.java:1285)
    at org.mockito.Mockito.mock(Mockito.java:1163)
    at com.example.android.ui.fragments.login.LoginPresenterTest.setUp(LoginPresenterTest.java:31)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    ... 1 more
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • I wonder if the fact that you use both `@Mock` and explicit calls to `mock()` might be related to the problem. Have you tried commenting out the calls to `mock()` in `setUp()`? It might be that one of the `mock()` calls fails because Dalvik requires some configuration info that your test runner provides when it handles `@Mock`. If that doesn't work, take a look at http://stackoverflow.com/questions/12267572/mockito-dexmaker-on-android?rq=1 and maybe try tweaking configuration, and/or try changing your versions of mockito and dexmaker, in case they're incompatible. – aro_tech Mar 23 '16 at 10:22
  • Yes I realized I was double injecting the dependencies and tried a) only using the `@Mock` annotation in combination with `MockitoAnnotations.initMocks` and b) no annotations and manually injecting mocks, like `loginView = Mockito.mock(LoginView.class);` but the error remains. I also tried using different versions of mockito and dexmaker, to no avail – Kaloyan Roussev Mar 23 '16 at 10:31

1 Answers1

0

Turns out I had the wrong dependencies for my test artifact in my gradle file.

I had this:

testCompile "com.crittercism.dexmaker:dexmaker:1.4"
testCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
testCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"

then I read somewhere that the dexmaker is only used for android instrumentation tests, and I was running Unit Tests, so when I removed these dependencies from my build.gradle file, I no longer experienced that problem

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180