MockitoAnnotations.initMocks在模拟()成功时崩溃 [英] MockitoAnnotations.initMocks crash while mock() succeeds

查看:1271
本文介绍了MockitoAnnotations.initMocks在模拟()成功时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工具测试中使用@Mock注释时遇到麻烦.

I'm having troubles using @Mock annotation with my instrumentation test.

这是我的gradle依赖项:

Here's my gradle dependencies:

androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

这是一段示例代码:

@Mock View mockView

@Before
public void setup() {
    MockitoAnnotation.initMocks(this);
    ...
}

此崩溃

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at com.google.dexmaker.mockito.DexmakerMockMaker.getInvocationHandlerAdapter(DexmakerMockMaker.java:80)
at com.google.dexmaker.mockito.DexmakerMockMaker.getHandler(DexmakerMockMaker.java:75)
...

但是,这可行

View mockView

@Before
public void setup() {
    mockView = Mockito.mock(View.class);
    ...
}

任何人都知道这里发生了什么吗?

Anyone has any ideas what's going on here?

推荐答案

它是dexmaker中的错误.我已提交拉取请求以进行修复: https://github.com/crittercism/dexmaker/pull/24

Its a bug in dexmaker. I have submitted a pull request to fix: https://github.com/crittercism/dexmaker/pull/24

请注意,您可以通过避免测试类中的成员变量为空来解决此问题.

Note that you may be able to work around it by avoiding null member variables in your test class.

这篇关于MockitoAnnotations.initMocks在模拟()成功时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆