gpt4 book ai didi

android - Kotlin android 应用程序 ExampleInstrumentedTest 不工作

转载 作者:行者123 更新时间:2023-11-29 18:32:57 25 4
gpt4 key购买 nike

使用 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)
...

用下面的gradle

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'
}

最佳答案

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

关于android - Kotlin android 应用程序 ExampleInstrumentedTest 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55389225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com