Mockito和JUnit未找到



我有一个非常古老的项目,它最初是一个日食项目,现在是gradle/android工作室

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

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"

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

我做错了什么?

出错的原因是配置

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

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

由于您的测试存储在src/test/java中,因此这些也包含在主要源代码中,因此您需要将配置从testImplementation更改为implementation进行编译。

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

此类错误可能是由于源集配置不正确而导致的。在您的build.gradle中,您有以下内容:

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
//snip
}
// Move the tests to tests/java, tests/res, etc...
androidTest.setRoot('tests')     
}

换句话说,您的Android测试(仪器测试(处于src/tests/java/com/example中。请注意,您还将main源集配置为所有src/*因此这也是不正确的 - 它应该只是src/main

确保您已正确理解本地单元测试(通常位于test目录中(和插桩单元测试(通常位于androidTest目录中(之间的区别,您还可以尝试将以下闭包添加到您的build.gradle

main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main'] 
}
test {
java.srcDirs = ['src/test']
}

然后,您可能需要执行 Gradle 同步,如果这不起作用,您可以尝试File -> Invalidate caches/restart.

祝你好运!

相关内容

  • 没有找到相关文章

最新更新