【问题标题】:Mockito Android with AndroidJUnitRunner error带有 AndroidJUnitRunner 错误的 Mockito Android
【发布时间】: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


【解决方案1】:

您应该将@RunWith(AndroidJUnit4.class) 更改为@RunWith(MockitoJUnitRunner.class。 此外,在您的 setup() 方法中,您可以添加这一行:courseWebService = mock(CourseWebService.class)

【讨论】:

  • 如果更改为@RunWith(MockitoJUnitRunner.class),则显示为Empty test suite
  • 尝试将androidTestCompile 'org.mockito:mockito-android:2.12.0'改为androidTestCompile 'org.mockito:mockito-core:2.11.0'
  • 这绝对不适合我。我也收到了Empty test suite.
  • 解决空测试套件错误,清理并再次制作项目。如果还是不行,关闭再重新导入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
相关资源
最近更新 更多