首页 > 解决方案 > 未找到 Mockito 和 JUnit

问题描述

我有一个非常古老的项目,它最初是一个 Eclipse 项目,现在是 gradle/android studio

目前我只有一个存储在路径中的测试

src/test/java/

我有以下 gradle 文件

buildscript {
    ext.kotlin_version = '1.2.41'
    ext.latest_google_version = '27.1.1'
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        maven { url 'https://dl.bintray.com/jetbrains/anko' }
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'io.fabric.tools:gradle:1.24.1'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'



kotlin {
    experimental {
        coroutines 'enable'
    }
}

repositories {
    maven { url "https://jitpack.io" }
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://dl.bintray.com/jetbrains/anko' }
    mavenCentral()
    google()
    jcenter()
}

dependencies {
    implementation fileTree(dir: 'libs', include: '*.jar')
    implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
        transitive = true
    }
    implementation('com.crashlytics.sdk.android:answers:1.4.1@aar') {
        transitive = true
    }
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.9'

    implementation 'com.jakewharton.rxbinding:rxbinding:0.4.0'
    implementation 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.anko:anko:0.10.3"
    implementation "com.android.support:support-v13:$latest_google_version"
    implementation "com.android.support:appcompat-v7:$latest_google_version"
    implementation "com.android.support:support-v4:$latest_google_version"
    implementation "com.android.support:support-core-utils:$latest_google_version"
    implementation "com.android.support:design:$latest_google_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'

    // UnitTest frameworks
    testImplementation 'junit:junit:4.12'
    testImplementation "org.mockito:mockito-core:2.18.0"
    testImplementation "org.robolectric:robolectric:3.8"

    // androidTest frameworks
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'

}

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        multiDexEnabled true
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/rxjava.properties'
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
//            resources.srcDirs = ['src']
//            aidl.srcDirs = ['src']
//            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
//            assets.srcDirs = ['assets']

        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

如果我使用上面的 gradle 文件运行单元测试,我会收到以下错误

Unresolved reference: junit 
Unresolved reference: junit 
Unresolved reference: mockito   
Unresolved reference: mockito   
Unresolved reference: mockito   
Unresolved reference: mockito   
Unresolved reference: Mock  
Unresolved reference: Before    
Unresolved reference: MockitoAnnotations    
Unresolved reference: Test  
Unresolved reference: verify    
Unresolved reference: verifyNoMoreInteractions  
Unresolved reference: Test  
Unresolved reference: Test

这在测试中引用 junit 的任何地方都有效。

如果我改变

// UnitTest frameworks
testImplementation 'junit:junit:4.12'
testImplementation "org.mockito:mockito-core:2.18.0"
testImplementation "org.robolectric:robolectric:3.8"

// UnitTest frameworks
implementation 'junit:junit:4.12'
implementation "org.mockito:mockito-core:2.18.0"
implementation "org.robolectric:robolectric:3.8"

测试编译但测试运行者会抱怨测试套件是空的。

我究竟做错了什么?

标签: androidjunitkotlinmockito

解决方案


您的错误的原因是配置

main {
    java.srcDirs = ['src']
}

在这里,您将整个src目录定义为包含Java主要源代码。默认目录是src/main/java

由于您的测试存储在src/test/java中,因此它们也包含在主要源中,因此您需要将配置从更改testImplementationimplementation编译。

您应该考虑重组项目以满足默认设置,而不是手动配置源集。


推荐阅读