Asserting View's text using Espresso succeed but perform(click()) seems didn't work.
My test code:
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class MainActivityTestRobo2 {
@Before
fun setUp() {
}
@Test
fun `test11`() {
val scenario = launchFragmentInContainer<MainFragment>()
scenario.recreate()
onView(withId(R.id.button1)).check(matches(withText("Button1"))) // success
onView(withId(R.id.message)).check(matches(withText("MainFragment"))) // success
onView(withId(R.id.button1)).perform(click()) // maybe not worked!
onView(withId(R.id.message)).check(matches(withText("Button1"))) // fail
}
}
Because that click() should have changed message's text to Button1 from MainFragment.
By the way, I wrote test code under test not under androidTest.
build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
viewBinding {
enabled = true
}
defaultConfig {
applicationId "com.example.tstester"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
compileOptions {
targetCompatibility = 1.8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
def fragment_version = "1.2.3"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'junit:junit:4.12'
testImplementation 'androidx.test.ext:junit:1.0.0'
testImplementation 'androidx.test:core:1.0.0'
testImplementation 'androidx.test.espresso:espresso-core:3.2.0'
debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Error message:
androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Button1"' doesn't match the selected view. Expected: with text: is "Button1" Got: "TextView{id=2131165295, res-name=message, visibility=VISIBLE, width=12, height=41, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@2673904a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=154.0, y=191.0, text=MainFragment, input-type=0, ime-target=false, has-links=false}"
androidx.test.espresso.ViewInteraction.postAsynchronouslyOnUiThread(ViewInteraction.java:312) at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:294) ... 17 more
Process finished with exit code 255