首页 > 解决方案 > 如何将库添加到 testImplementation 和 androidTestImplementation

问题描述

用于testand android tests、单元测试和 UI 测试的 kotlin 库。

如果我将下面两行作为 build.gradle 的一部分编写,它可以正常工作。

我的问题是,这是添加要在测试和 android 测试中使用的库的正确方法吗

构建.gradle

   androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
    testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"

感谢您的宝贵建议

谢谢R

标签: androidmockitobuild.gradlemockito-kotlin

解决方案


这是添加要在测试和android测试中使用的库的正确方法吗

一般来说,是的,只要你需要一个androidTestImplementation和一个testImplementation语句,即使它们都用于同一个库。

如果你想让库保持同步,你可以为库的 Maven 坐标定义一个常量:

def mockLibrary = "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"

// TODO other good stuff here

dependencies {
// TODO other great libraries here
// TODO OK, maybe a few not-so-great libraries too
  androidTestImplementation mockLibrary
  testImplementation mockLibrary
}

推荐阅读