无法在Android Studio中导入Mockito

91zkwejq  于 9个月前  发布在  Android
关注(0)|答案(6)|浏览(106)

我已经开始为我的应用程序编写单元测试。我在用Mockito来模拟物体。
This是我在应用程序级别的gradle文件中包含mockito依赖项所遵循的链接。问题是我无法将mockito导入到我的测试类中。
这是我的app build.gradle文件供参考。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    lintOptions {
        disable 'HardcodedText','TextFields','OnClick'
    }
}

repositories {
    jcenter()
    mavenCentral()
    maven() {
        name 'SonaType snapshot repository'
        url 'https://oss.sonatype.org/content/repositories/snapshots'
    }
}

ext {
    robobindingVersion = 'latest.integration'
    //robobindingVersion = '0.8.6-SNAPSHOT'
}

dependencies {
    testCompile "org.mockito:mockito-core:1.+"
    compile("org.robobinding:robobinding:$robobindingVersion:with-dependencies") {
        exclude group: 'com.google.guava', module: 'guava'
    }
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    apt "org.robobinding:codegen:$robobindingVersion"
    compile 'junit:junit:4.12'
}

gr8qqesn

gr8qqesn1#

这个问题很久以前就被问到了,我想它对提问者来说已经不相关了。我看到了很多可能有效或解决个人问题的答案,正确的答案就在其中。然而,我还没有看到任何答案来解释为什么原始问题的提问者无法导入mockito。
原因是他们将对mockito的依赖声明为testCompile。接下来,他们试图将它们导入InstrumentationTestCase中。它们具有不同的依赖集。OP应该使用androidTestCompile声明依赖性。
此外,自从提出这个问题以来,关键词已经改变。现在我们使用implementationapi来声明模块依赖关系。
当您不需要访问所依赖的模块的依赖项时,请使用implementation。你会使用它的大多数时间。如果您确实需要模块依赖于您自己的依赖项,请使用api
同样,如果在模块逻辑中需要前缀,请不要使用前缀。如果您需要本地单元测试代码的依赖项(testImplementation "org.mockito..."),请使用test前缀;如果您需要在插装测试中使用androidTest前缀(androidTestImplementation "org.mockito..."),请使用androidTest前缀。

lf3rwulv

lf3rwulv2#

检查此链接....这可能有用
https://stackoverflow.com/a/34148132/5185201
// add dexOptions

dexOptions {
    incremental true
    javaMaxHeapSize "4g"
}

//启用多索引支持。

multiDexEnabled true
yxyvkwin

yxyvkwin3#

手动导入mockito jar文件为我做了这件事。为此,首先在应用程序目录中创建一个名为“libs”的目录。请注意,此目录应与src/main和build目录处于同一级别。接下来,下载mockito jar文件并将其粘贴到libs目录中。
将其包含到应用级build.gradle文件中的依赖项中:

dependencies {
    compile files('libs/add-your-jar-file-name-here')
}

同步gradle和这应该做的工作。
有关快照的更详细答案,请参阅此答案。

7rfyedvj

7rfyedvj4#

尝试更改
compile "org.mockito:mockito-core:1.+"implementation "org.mockito:mockito-core:1.+"

androidTestImplementation "org.mockito:mockito-core:1.+"

vsikbqxv

vsikbqxv5#

我遇到了同样的问题,并通过使用来解决它:
compile "org.mockito:mockito-core:1.+"
希望有帮助=]

jq6vz3qz

jq6vz3qz6#

在我的例子中,我通过将依赖级别从“testCompile”修改为“implementation”来解决它。

implementation 'org.mockito:mockito-core:5.3.1'

相关问题