@RunWith(JUnit4::class)
class CrimeDetailFragmentTest {
private lateinit var scenario: FragmentScenario<CrimeDetailFragment>
@Before
fun setup() {
scenario = launchFragment()
}
@After
fun tearDown() {
scenario.close()
}
@Test fun checkbox_updates_crime() {
onView(withId(R.id.chbx_solved))
.perform(click())
scenario.onFragment {
onView(withId(R.id.chbx_solved))
.check(matches(isChecked()))
}
}
}
Right now the class fails to run on either the emulator or a physical device - the build continues indefinitely and the only hint of any problem is the error [ddms]: Exception during activity from Selector.
Use launchFragmentInContainer instead of launchFragment when creating your scenario. If you want to test your UI within your Fragment you need to use that API. See: Test your fragments | Android Developers
Do not use scenario.onFragment {} when doing your check. That function is only needed if you need to call functions or access properties within the Fragment. You do not need it to perform Espresso validations. Pull that onView()... code out of that scenario.onFragment {} lambda expression and just invoke it the same way that you did when you clicked the checkbox.
Hmmmmm, that’s weird. Can you try running the tests from the command line? Try this command from within the project folder and let me know what output you get:
[0] ./gradlew connectedCheck --stacktrace
Starting a Gradle Daemon, 1 stopped Daemon could not be reused, use --status for details
Task :app:compileDebugKotlin
w: /Users/USERNAME/Magic/Android/CriminalIntent/app/src/main/java/com/example/criminalintent/detail/CrimeDetailFragment.kt: (61, 69): Parameter ‘b’ is never used, could be renamed to _
<============-> 96% EXECUTING [38s]
:app:connectedDebugAndroidTest
IDLE
IDLE
Hmmmmmmm. Is there any chance you still have the “Don’t keep Activities” developer option enabled? That might cause issues here. Also, when running the test, do you see your Fragment briefly flash on the device’s screen? Are there any crashes that you are seeing in Logcat?
I’m sorry this is being so tricky to debug, but this is pretty strange and I’m not quite sure what is happening just yet.