Mockito.mock()因NPE失败:NoSuchMethodError:java.lang.System.logW

时间:2016-03-23 09:06:05

标签: android mockito

这是我的测试类:

@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被嘲笑得很好(它是一个接口)但是试图模拟LoginNetworkOperation会引发这个错误:

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

1 个答案:

答案 0 :(得分:0)

原来我的gradle文件中的测试工件存在错误的依赖关系。

我有这个:

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

然后我在某处读到dexmaker仅用于android检测,我正在运行单元测试,所以当我从build.gradle文件中删除这些依赖项时,我不再遇到这个问题