Kotlin android应用程序ExampleInstrumentedTest无法正常工作

时间:2019-03-28 02:17:13

标签: android unit-testing kotlin

使用Android Studio创建了一个新的Kotlin android应用,并尝试按以下方式运行示例检测测试...下面的gradle文件及其错误?不确定如何解决。我尝试将上下文更改为         val context:上下文=             ApplicationProvider.getApplicationContext(),但这只会给出一个不同的错误(类似的错误,但不同的错误。

package com.mycomp.myprog

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().context
        assertEquals("com.mycomp.myprog", appContext.packageName)
    }
}

我收到以下错误

org.junit.ComparisonFailure: expected:<com.mycomp.myprog[]> but was:<com.mycomp.myprog[.test]>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
...

具有以下优点

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.mycomp.myprog"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
    implementation 'androidx.core:core-ktx:1.1.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    //testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'

    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test:core:1.1.1-alpha02'

    // JUnit-4 framework (See vintage below)
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.3.20'

    // JUnit-5 Jupiter
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.4.0'

    // JUnit-5 (Vintage give access to JUnit-4)
    testImplementation "junit:junit:4.12"
    testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.4.0'
}

1 个答案:

答案 0 :(得分:2)

InstrumentationRegistry.getInstrumentation().context是检测应用程序的上下文。从您的断言中,您需要目标工具的上下文,即val appContext = InstrumentationRegistry.getInstrumentation().targetContext