Mockito not Mocking in Instrumented Android Test [duplicate]

▼魔方 西西 提交于 2020-01-05 04:01:30

问题


EDIT: Solution: Changed Mockito Version to 1.+.

In my androidTest folder I have something like this:

@Before
public void setUp() throws Exception {
    LoginPresenter context = Mockito.mock(LoginPresenter.class);
}

I got the following dependencies:

androidTestCompile "org.mockito:mockito-core:2.2.9"
androidTestCompile "com.android.support.test:runner:0.5"
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
androidTestCompile "com.android.support.test.espresso:espresso-intents:2.2.2"
testCompile "org.mockito:mockito-core:1.+"

The mocking does not work though. I get the following stacktrace:

I/TestRunner: java.lang.AbstractMethodError: abstract method "org.mockito.plugins.MockMaker$TypeMockability org.mockito.plugins.MockMaker.isTypeMockable(java.lang.Class)"
                  at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
                  at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
                  at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:168)
                  at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:162)
                  at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
                  at org.mockito.Mockito.mock(Mockito.java:1632)
                  at org.mockito.Mockito.mock(Mockito.java:1545)
                  at lucanet.de.orderapp.login.LoginActivityTest.setUp(LoginActivityTest.java:38)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
                  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
                  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
                  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
                  at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
                  at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
                  at org.junit.rules.RunRules.evaluate(RunRules.java:20)
                  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
                  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
                  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
                  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                  at org.junit.runners.Suite.runChild(Suite.java:128)
                  at org.junit.runners.Suite.runChild(Suite.java:27)
                  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                  at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                  at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                  at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
                  at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
                  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
I/TestRunner: ----- end exception -----

Can anybody explain, what I am missing? I do not understand what the problem is. I tried different versions of Mockito, changed the DexMaker to a different one. I am following this tutorial, where it seems to work fine: https://medium.com/@fabioCollini/android-testing-using-dagger-2-mockito-and-a-custom-junit-rule-c8487ed01b56#.tal0hxir6

The same line of code works perfectly fine in /test


回答1:


Change mockito-core version to 2.0.17-beta. Unfortunately, the dexmaker does not work with higher Mockito version. Described more here.



来源:https://stackoverflow.com/questions/40401659/mockito-not-mocking-in-instrumented-android-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!