【问题标题】:How can I use Mockito in my Android app itself? I'm receiving Hamcrest dependency conflicts that I don't receive when it's used only in tests如何在我的 Android 应用程序中使用 Mockito?我收到了仅在测试中使用时没有收到的 Hamcrest 依赖冲突
【发布时间】:2016-11-23 21:41:24
【问题描述】:

我想在我正在编写的应用程序的发布版本中使用 Mockito 的 mock()clone() 方法,而不仅仅是在测试中。 (是的,我知道测试是什么,我不担心性能,而且我有a reason for this。)但是当我将 Mockito 作为运行时依赖项包含在内时,我的项目无法编译。它使用 Mockito 1.10.19、Dexmaker 1.4 和 Espresso 2.2.2 编译为 app/build.gradle 中的测试依赖项:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 25
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}
dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'

    testCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'
    androidTestCompile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
    androidTestCompile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'
}

但是当我尝试将这些更改为运行时依赖项时:

compile 'org.mockito:mockito-core:1.10.19'
compile 'com.crittercism.dexmaker:dexmaker:1.4'
compile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
compile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'

Gradle 在prepareDebugUnitTestDependencies 任务期间尝试使用buildassembleAndroidTest 任务构建项目时返回以下错误:

与依赖项“org.hamcrest:hamcrest-core”冲突。应用程序 (1.1) 和测试应用程序 (1.3) 的已解决版本不同。详情请见http://g.co/androidstudio/app-test-app-conflict

我可以在dependencies Gradle 任务中看到 Mockito 1.10.19 依赖于 Hamcrest 1.1,而 JUnit 4.12 依赖于 Hamcrest 1.3,但这个错误对我来说很奇怪,原因有两个:

  1. 当它们是testCompileandroidTestCompile 依赖项时,我没有收到此错误,但当时它们都在使用。在我看来,我没有添加任何新的测试依赖项,那么为什么它们现在会发生冲突?
  2. assembleReleaseassembleDebug 完成,没有任何错误。

如何在我的应用程序的测试和发布版本中使用 Mockito?

我尝试了几件事,但没有成功:

  • 复制 Mockito 和 Dexmaker 依赖项,将它们指定为普通依赖项和测试依赖项。

  • 使用 Mockito 2。我尝试了当前版本 2.2.22 和 2.1.0,但是尝试在 JUnit 测试中使用 mock() 时遇到了类似的错误:

    java.lang.AbstractMethodError: com.android.dx.mockito.DexmakerMockMaker.isTypeMockable(Ljava/lang/Class;)Lorg/mockito/plugins/MockMaker$TypeMockability;
    
        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:1637)
        at org.mockito.Mockito.mock(Mockito.java:1550)
    

    从我在网上可以找到的信息来看,目前 Mockito 2.x 似乎没有办法在 Android 上运行。

【问题讨论】:

    标签: android android-gradle-plugin mockito junit4 gradle-dependencies


    【解决方案1】:

    我能够通过使用某些依赖项的旧版本来实现此功能:JUnit 4.10 和 Espresso 2.1。这会将 Hamcrest 1.3 的依赖关系更改为 Hamcrest 1.1,以便每个人都使用 1.1。在我的真实项目中(不是我问题中的测试示例),我还必须使用 junit-dep 工件而不是 junit 工件。 (这可能是因为a difference between the JUnit artifacts for versions 4.10 and 4.11+,其中junit:4.10 工件包含一些Hamcrest 类,而junit-dep:4.10junit:4.11+ 工件没有。)所以依赖关系变为:

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit-dep:4.10'
    
    compile 'org.mockito:mockito-core:1.10.19'
    compile 'com.crittercism.dexmaker:dexmaker:1.4'
    compile 'com.crittercism.dexmaker:dexmaker-dx:1.4'
    compile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'
    

    【讨论】:

    • 这适用于我的目的(至少到目前为止)。但是我不知道为什么当 Mockito 是 androidTestCompile 依赖项时,androidTest 测试会使用这些所谓的冲突依赖项正确构建和运行。如果有人可以解释原因,或者知道如何让这些软件包的更新版本一起工作,请也写一个答案!
    猜你喜欢
    • 2015-04-22
    • 1970-01-01
    • 2016-01-23
    • 2020-07-30
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多