Android:运行测试用例时:无法确定任务':app:compileDebugAndroidTestJavaWithJavac'的依赖关系

Posted

技术标签:

【中文标题】Android:运行测试用例时:无法确定任务\':app:compileDebugAndroidTestJavaWithJavac\'的依赖关系【英文标题】:Android: While running test cases: Could not determine the dependencies of task ':app:compileDebugAndroidTestJavaWithJavac'Android:运行测试用例时:无法确定任务':app:compileDebugAndroidTestJavaWithJavac'的依赖关系 【发布时间】:2020-04-03 13:40:22 【问题描述】:

我最近说过要编写测试用例。我正在使用 JUnit 和 Barista。

我的应用程序编译成功并正在运行,但是当我运行测试用例时,它显示以下错误:

Could not determine the dependencies of task ':app:compileDebugandroidTestJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestCompileClasspath'.
   > Could not find org.mockito:mockito-core:3.0.6.
     Required by:
         project :app
   > Could not find org.mockito:mockito-android:3.0.6.
     Required by:
         project :app
   > Could not find org.mockito:mockito-core:3.0.6.
     Required by:
         project :app > com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0
   > Could not find org.mockito:mockito-core:3.0.6.
     Required by:
         project :app > androidx.arch.core:core-testing:2.0.0
   > Could not find org.mockito:mockito-core:3.0.6.
     Required by:
         project :app > org.mockito:mockito-inline:2.21.0

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

我不确定为什么会发生这种情况,我在这里遗漏了一件小事。

我已经尝试了有关堆栈溢出的所有其他类似问题,但问题仍然存在。

这是我的应用级 build.gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'
//Added for Epoxy
kapt 
    correctErrorTypes = true

android 
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    dataBinding 
        enabled = true
    

    compileOptions 
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    

    defaultConfig 
        applicationId "com.example.githubdemo"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner = "com.example.githubdemo.TestRunner"
    

    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
        debug
        uat
    



dependencies 

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    // ReactiveX
    implementation "io.reactivex.rxjava2:rxjava:$rxjava_version"
    implementation "io.reactivex.rxjava2:rxkotlin:$rxkotlin_version"
    implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"

    // Rx
    implementation("com.jakewharton.rxbinding2:rxbinding-kotlin:2.2.0")
    implementation("com.jakewharton.rxbinding2:rxbinding-support-v4-kotlin:2.2.0")
    implementation("com.jakewharton.rxbinding2:rxbinding-appcompat-v7-kotlin:2.2.0")
    implementation("com.patloew.rxlocation:rxlocation:1.0.5")
    androidTestImplementation 'com.squareup.rx.idler:rx2-idler:0.10.0'

    // Retrofit2
    implementation "com.squareup.retrofit2:retrofit:$retrofit2_version"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit2_version"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit2_version"

    // Picasso
    implementation "com.squareup.picasso:picasso:$picasso_version"

    // Koin
    implementation "org.koin:koin-android-viewmodel:2.0.1"

    def nav_version = "2.1.0"
    implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
    implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-fragment:2.1.0"
    implementation "androidx.navigation:navigation-ui-ktx:2.1.0"
    implementation "androidx.navigation:navigation-fragment-ktx:2.1.0"



    //Airbnb Epoxy
    def epoxyVersion = "3.9.0"
    implementation "com.airbnb.android:epoxy:$epoxyVersion"
    kapt "com.airbnb.android:epoxy-processor:$epoxyVersion"
    implementation "com.airbnb.android:epoxy-databinding:$epoxyVersion"

    //MvRx
    implementation 'com.airbnb.android:mvrx:2.0.0-alpha2'
    testImplementation 'com.airbnb.android:mvrx-testing:2.0.0-alpha2'
    androidTestImplementation("com.airbnb.android:mvrx-testing:2.0.0-alpha2")


    // Testing Libs

    androidTestImplementation('org.mockito:mockito-core:3.0.6')
    androidTestImplementation('org.mockito:mockito-android:3.0.6')
    androidTestImplementation('com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0')

    testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.2')
    testImplementation('org.spekframework.spek2:spek-dsl-jvm:2.0.8')
    testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.5.2')
    testRuntimeOnly('org.spekframework.spek2:spek-runner-junit5:2.0.8')

    debugImplementation('androidx.fragment:fragment-testing:1.2.0-rc02')

    androidTestImplementation('androidx.arch.core:core-testing:2.0.0')
    androidTestImplementation('androidx.test.ext:junit:1.1.0')
    androidTestImplementation('androidx.test:runner:1.1.0')
    androidTestImplementation('androidx.test:core:1.1.0')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.1')

    androidTestImplementation('androidx.test.espresso:espresso-idling-resource:3.1.1')
    androidTestImplementation('androidx.test.espresso.idling:idling-concurrent:3.2.0')
    androidTestImplementation('androidx.test.espresso:espresso-contrib:3.1.1')
    androidTestImplementation('androidx.test.espresso:espresso-intents:3.1.1')

    androidTestImplementation('com.schibsted.spain:barista:3.2.0')
    androidTestImplementation('org.opentest4j:opentest4j:1.2.0')
    androidTestImplementation('com.squareup.okhttp3:mockwebserver:4.2.2')
    androidTestImplementation('com.jakewharton.espresso:okhttp3-idling-resource:1.0.0')

    // Koin
    androidTestImplementation('org.koin:koin-test:2.0.1')

    // Recyclerview Child Actions for Espresso
    implementation('it.xabaras.android.espresso:recyclerview-child-actions:1.0')

    //Input mask
    implementation('com.redmadrobot:input-mask-android:5.0.0')

这是我的项目级 build.gradle 文件:

buildscript 
    ext.kotlin_version = "1.3.61"
    ext.rxjava_version = '2.2.10'
    ext.rxkotlin_version = '2.4.0'
    ext.rxandroid_version = '2.1.1'
    ext.retrofit2_version = '2.6.2'
    ext.picasso_version = '2.71828'

    repositories 
        google()
        jcenter()
        mavenCentral()
        maven 
            url 'https://maven.google.com'
        
        maven
            url  'https://jitpack.io'
        
    
    dependencies 
        classpath "com.android.tools.build:gradle:4.0.0-alpha07"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0'
        classpath 'com.google.gms:google-services:4.2.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    


allprojects 
    repositories 
        google()
        jcenter()
    


task clean(type: Delete) 
    delete rootProject.buildDir

我错过了什么吗?

【问题讨论】:

【参考方案1】:

您是否尝试从 build.gradle 中删除多余的依赖项并尝试重新构建项目。我遇到了类似的问题,并通过这个来解决

【讨论】:

感谢您的回答,但为时已晚,但您是对的。这就是问题所在,我能够通过从 build.gradle 中删除一些依赖来解决这个问题,接受你的答案,这样它可能对其他人有帮助。 @nisha 请尝试自信地表达答案,而不是以问题的形式。

以上是关于Android:运行测试用例时:无法确定任务':app:compileDebugAndroidTestJavaWithJavac'的依赖关系的主要内容,如果未能解决你的问题,请参考以下文章

如何在 bitrise 上运行测试用例时查看模拟器

运行 Junit 测试用例时“加载 ApplicationContext 失败”

逻辑覆盖

当我在 Eclipse 中运行 Junit 测试用例时,出现错误

在使用 Jasmine Karma 编写测试用例时,我无法涵盖正在执行的处理程序

无法使用 detox 运行测试用例