首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin android应用程序ExampleInstrumentedTest不工作

Kotlin android应用程序ExampleInstrumentedTest不工作
EN

Stack Overflow用户
提问于 2019-03-28 02:17:13
回答 1查看 1.2K关注 0票数 2

使用android创建了一个新的Kotlin Android应用程序,并试图运行下面的示例工具测试。下面的gradle文件和它的错误?不知道怎么解决。我尝试将上下文更改为val : context = ApplicationProvider.getApplicationContext(),但这只会产生不同的错误(类似的错误,但不同的错误)。

代码语言:javascript
复制
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)
    }
}

我得到以下错误

代码语言:javascript
复制
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)
...

用下面的分度

代码语言:javascript
复制
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'
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-28 08:44:36

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55389225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档