【发布时间】:2017-11-28 00:37:27
【问题描述】:
我正在尝试将 Mockito Android 与 AndroidJUnitRunner 一起使用并出现错误。我尝试了许多其他 gradle 依赖项组合,但没有运气。我哪里错了?
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
这里是 gradle 测试编译块
testCompile 'junit:junit:4.12'
androidTestCompile 'org.mockito:mockito-android:2.12.0'
androidTestCompile('com.android.support.test:runner:+', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.google.code.findbugs'
})
我遇到了错误
org.mockito.exceptions.base.MockitoException:Mockito 无法模拟 这个类:接口 *.CourseWebService。
Mockito 只能模拟非私有和非最终类。如果你不是 确定您收到此错误的原因,请向邮件列表报告。
适用于 Android 用户的重要信息:
常规的 Byte Buddy 模拟制造商无法在 Android 上生成代码 虚拟机!要解决此问题,请使用 'mockito-android' 依赖项 你的申请: http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22mockito-android%22%20g%3A%22org.mockito%22
Java:0.9 JVM 供应商名称:Android 项目 JVM 供应商版本:2.1.0 JVM 名称:Dalvik JVM 版本
:0.9 JVM 信息:空操作系统名称:Linux 操作系统版本 : 3.18.31-perf-gcd8e090基础异常:
com.android.dex.util.ExceptionWithContext: com.android.dx.cf.attrib.RawAttribute cannot be cast to com.android.dx.cf.attrib.AttSignature
这里是测试类:
@RunWith(AndroidJUnit4.class)
public class CourseTest {
@Mock
CourseWebService courseWebService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
gson = new NetworkingConfigurationModule().provideGson();
assertNotNull(gson);
}
}
【问题讨论】:
-
CourseWebService 类是私有的还是最终的?
-
@andrei 当然不,只是一个公共接口
标签: android unit-testing mockito