Kotlin android应用程序示例InstrumentedTest不工作

Kotlin android应用程序示例InstrumentedTest不工作,android,unit-testing,kotlin,Android,Unit Testing,Kotlin,使用android Studio创建了一个新的Kotlin android应用程序,并尝试运行以下示例检测测试。。。下面的gradle文件及其错误?不知道怎么解决。我试着将上下文更改为 val上下文:上下文= ApplicationProvider.getApplicationContext()但这只是给出了一个不同的错误(类似的错误,但不同的错误) package com.mycomp.myprog import androidx.test.ext.junit.runners.Android

使用android Studio创建了一个新的Kotlin android应用程序,并尝试运行以下示例检测测试。。。下面的gradle文件及其错误?不知道怎么解决。我试着将上下文更改为 val上下文:上下文= 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)
...

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

Hmm好的,但是在使用它时,我得到了这个错误,而不是
java.lang.AssertionError:expected:but was:org.junit.Assert.fail(Assert.java:88)org.junit.Assert.failNotEquals(Assert.java:834)org.junit.Assert.assertEquals(Assert.java:118)org.junit.Assert.assertEquals(Assert.java:144)在com.mycomp.myprog.ExampleInstrumentedTest.useAppContext(ExampleInstrumentedTest.kt:24)
Oh,我明白了,然后你必须将断言更新为这个
assertEquals(“com.mycomp.myprog”,appContext.packageName)
,这样它才能工作。:)
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'
}