I have an activity with a Button in it. On clicking the button, am calling finish(). The problem is, when I am writing an instrumentation test to check if the activity destroyed, I am getting
junit.framework.AssertionFailedError: expected:<DESTROYED> but was:<RESUMED>
My questions are
- Why the state is still
RESUMEDeven afterfinish()got called? - Isn't it the right method to check the activity state?
Here's my activity
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.b_kill_me).setOnClickListener {
finish()
}
}
}
Here's my test
MainActivityTest.kt
class MainActivityTest {
@Test
fun killMe() {
val mainActivity = ActivityScenario.launch(MainActivity::class.java)
onView(withId(R.id.b_kill_me)).perform(click())
assertEquals(Lifecycle.State.DESTROYED, mainActivity.state)
}
}
Here's the complete source code : Google Drive Link