运行 Junit 5 Android 测试时未解决的引用 jupiter 和 assertTrue
Unresolved reference jupiter and assertTrue when running Junit 5 Android tests
我正在尝试在处理 Bitmap
的 Kotlin 中创建一个 Android 测试 class 但由于这些错误我无法 运行 测试.它们只发生在 androidTest 中的任何测试 class 但 test 运行 中的简单 JVM 测试没有问题。
首先,这是我的测试 class 的样子
import android.graphics.Bitmap
import androidx.test.core.app.ApplicationProvider
import org.junit.jupiter.api.Assertions.assertTrue
class RoundImageTest {
@org.junit.jupiter.api.Test
fun imagesRatio() {
// test with square images
val squareBitmap: Bitmap = Bitmap.createBitmap(
164, 164, Bitmap.Config.ARGB_8888
)
assertTrue(squareBitmap.height == squareBitmap.width)
}
}
按照指定的说明进行操作 here 我在我的项目 build.gradle
中有这个
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'eu.appcom.gradle:android-versioning:1.0.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.10.0"
classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"
classpath("de.mannodermaus.gradle.plugins:android-junit5:1.6.2.0")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
这在我的应用程序 build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'eu.appcom.gradle.android-versioning'
apply plugin: 'maven'
apply plugin: 'kotlin-kapt'
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.demoapp"
minSdkVersion 21
targetSdkVersion 29
versionCode versioning.getVersionCode()
versionName versioning.getVersionName()
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"room.schemaLocation": "$projectDir/schemas" . toString(),
"room.incremental": "true",
"room.expandProjection": "true"
]
}
}
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArgument("runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder")
}
testBuildType "alpha"
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
androidTest.assets.srcDirs += files("$projectDir/schemas" . toString())
}
signingConfigs {
debug {
keyAlias keystoreProperties['debugKeyAlias']
keyPassword keystoreProperties['debugKeyPassword']
storeFile file(rootDir.getCanonicalPath() + '/' + keystoreProperties['debugKeyStore'])
storePassword keystoreProperties['debugStorePassword']
}
release {
keyAlias keystoreProperties['releaseKeyAlias']
keyPassword keystoreProperties['releaseKeyPassword']
storeFile file(rootDir.getCanonicalPath() + '/' + keystoreProperties['releaseKeyStore'])
storePassword keystoreProperties['releaseStorePassword']
}
}
buildTypes {
alpha {
applicationIdSuffix ".alpha"
debuggable true
minifyEnabled false
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
packagingOptions {
exclude 'META-INF/atomicfu.kotlin_module'
exclude "META-INF/LICENSE*"
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// AndroidX
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'androidx.test:core:1.3.0'
// ACRA
implementation "ch.acra:acra-mail:$acra_version"
implementation "ch.acra:acra-notification:$acra_version"
// Room components
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
androidTestImplementation "androidx.room:room-testing:$room_version"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
androidTestImplementation "androidx.arch.core:core-testing:$androidx_version"
// ViewModel Kotlin support
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// Co-routines
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
// Material UI
implementation "com.google.android.material:material:$material_version"
// LeakCanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
alphaImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
// Navigation
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
// Junit
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
androidTestImplementation "androidx.test:runner:1.3.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2'
androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.2.0"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.0"
// Espresso
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
}
现在,当我尝试运行测试时,这就是构建输出显示给我的
Task :app:compileAlphaAndroidTestKotlin FAILED
e: RoundImageTest.kt: (5, 18): Unresolved reference: jupiter
e: RoundImageTest.kt: (9, 16): Unresolved reference: jupiter
e: RoundImageTest.kt: (16, 9): Unresolved reference: assertTrue
e: RoundImageTest.kt: (23, 9): Unresolved reference: assertTrue
e: RoundImageTest.kt: (30, 9): Unresolved reference: assertTrue
显然,我要做的是改变
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
至
androidTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
我正在尝试在处理 Bitmap
的 Kotlin 中创建一个 Android 测试 class 但由于这些错误我无法 运行 测试.它们只发生在 androidTest 中的任何测试 class 但 test 运行 中的简单 JVM 测试没有问题。
首先,这是我的测试 class 的样子
import android.graphics.Bitmap
import androidx.test.core.app.ApplicationProvider
import org.junit.jupiter.api.Assertions.assertTrue
class RoundImageTest {
@org.junit.jupiter.api.Test
fun imagesRatio() {
// test with square images
val squareBitmap: Bitmap = Bitmap.createBitmap(
164, 164, Bitmap.Config.ARGB_8888
)
assertTrue(squareBitmap.height == squareBitmap.width)
}
}
按照指定的说明进行操作 here 我在我的项目 build.gradle
中有这个dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'eu.appcom.gradle:android-versioning:1.0.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.10.0"
classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"
classpath("de.mannodermaus.gradle.plugins:android-junit5:1.6.2.0")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
这在我的应用程序 build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'eu.appcom.gradle.android-versioning'
apply plugin: 'maven'
apply plugin: 'kotlin-kapt'
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.example.demoapp"
minSdkVersion 21
targetSdkVersion 29
versionCode versioning.getVersionCode()
versionName versioning.getVersionName()
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"room.schemaLocation": "$projectDir/schemas" . toString(),
"room.incremental": "true",
"room.expandProjection": "true"
]
}
}
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArgument("runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder")
}
testBuildType "alpha"
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
androidTest.assets.srcDirs += files("$projectDir/schemas" . toString())
}
signingConfigs {
debug {
keyAlias keystoreProperties['debugKeyAlias']
keyPassword keystoreProperties['debugKeyPassword']
storeFile file(rootDir.getCanonicalPath() + '/' + keystoreProperties['debugKeyStore'])
storePassword keystoreProperties['debugStorePassword']
}
release {
keyAlias keystoreProperties['releaseKeyAlias']
keyPassword keystoreProperties['releaseKeyPassword']
storeFile file(rootDir.getCanonicalPath() + '/' + keystoreProperties['releaseKeyStore'])
storePassword keystoreProperties['releaseStorePassword']
}
}
buildTypes {
alpha {
applicationIdSuffix ".alpha"
debuggable true
minifyEnabled false
signingConfig signingConfigs.debug
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
packagingOptions {
exclude 'META-INF/atomicfu.kotlin_module'
exclude "META-INF/LICENSE*"
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// AndroidX
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'androidx.test:core:1.3.0'
// ACRA
implementation "ch.acra:acra-mail:$acra_version"
implementation "ch.acra:acra-notification:$acra_version"
// Room components
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
androidTestImplementation "androidx.room:room-testing:$room_version"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
androidTestImplementation "androidx.arch.core:core-testing:$androidx_version"
// ViewModel Kotlin support
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// Co-routines
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
// Material UI
implementation "com.google.android.material:material:$material_version"
// LeakCanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
alphaImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
// Navigation
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
// Junit
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
androidTestImplementation "androidx.test:runner:1.3.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2'
androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.2.0"
androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.2.0"
// Espresso
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
}
现在,当我尝试运行测试时,这就是构建输出显示给我的
Task :app:compileAlphaAndroidTestKotlin FAILED
e: RoundImageTest.kt: (5, 18): Unresolved reference: jupiter
e: RoundImageTest.kt: (9, 16): Unresolved reference: jupiter
e: RoundImageTest.kt: (16, 9): Unresolved reference: assertTrue
e: RoundImageTest.kt: (23, 9): Unresolved reference: assertTrue
e: RoundImageTest.kt: (30, 9): Unresolved reference: assertTrue
显然,我要做的是改变
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
至
androidTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'