Reputation: 673
Created a new Kotlin android app using Android Studio and trying to run the example instrumentation test as follows... with the gradle file below and its erroring? Not sure how to resolve. I tried changing the context to val context: Context = ApplicationProvider.getApplicationContext() but that just gives a different error (similar error but different one.
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)
}
}
I get the following error
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)
...
with the following 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'
}
Upvotes: 2
Views: 1640
Reputation: 17085
InstrumentationRegistry.getInstrumentation().context
is the context of the instrumentation app. From your assert you want the context of the target instrumentation, which is val appContext = InstrumentationRegistry.getInstrumentation().targetContext
Upvotes: 4