Reputation: 191
I have a fragment that starts an activity with an intent. I want to write a unit test for this using Espresso UI
. I know that Espresso contains a class for testing intents called IntentsTestRule
. The only problem with this rule is that it only works for activities and not fragments because its constructors don't accept a subtype of Fragment
type.
@RunWith(AndroidJUnit4::class)
@Config(application = TestApplication::class)
class MyFragmentSpec {
@Test
fun `On next button clicked send intent to start activity`() {
launchFragmentInContainer<MyFragment>()
onView(withId(R.id.edit_text)).perform(typeText("HelloWorld\n"))
onView(withId(R.id.next_button)).perform(click())
Thread.sleep(2000L)
intended(hasAction("com.my.application.randomactitivy"))
}
}
My test fails with the following exception
com.my.application.MyFragmentSpec > On next button clicked send intent to start activity FAILED
java.lang.NullPointerException
at androidx.test.espresso.intent.Intents$2.check(Intents.java:194)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:425)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:288)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:272)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.$$robo$$android_os_Handler$handleCallback(Handler.java:873)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$dispatchMessage(Handler.java:99)
at android.os.Handler.dispatchMessage(Handler.java)
at org.robolectric.shadows.ShadowLegacyMessageQueue.dispatchMessage(ShadowLegacyMessageQueue.java:157)
at org.robolectric.shadows.ShadowLegacyMessageQueue.access$200(ShadowLegacyMessageQueue.java:42)
at org.robolectric.shadows.ShadowLegacyMessageQueue$1.run(ShadowLegacyMessageQueue.java:135)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:359)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:163)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:152)
at org.robolectric.shadows.ShadowLegacyMessageQueue.enqueueMessage(ShadowLegacyMessageQueue.java:142)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.$$robo$$android_os_Handler$enqueueMessage(Handler.java:745)
at android.os.Handler.enqueueMessage(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$sendMessageAtTime(Handler.java:697)
at android.os.Handler.sendMessageAtTime(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$sendMessageDelayed(Handler.java:667)
at android.os.Handler.sendMessageDelayed(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$post(Handler.java:395)
at android.os.Handler.post(Handler.java)
at androidx.test.espresso.base.BaseLayerModule$1.execute(BaseLayerModule.java:92)
at androidx.test.espresso.ViewInteraction.postAsynchronouslyOnUiThread(ViewInteraction.java:312)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:294)
at androidx.test.espresso.intent.Intents.intended(Intents.java:190)
at androidx.test.espresso.intent.Intents.intended(Intents.java:170)
at com.my.application.MyFragmentSpec.On next button clicked send intent to start activity(MyFragmentSpec.kt:107)
Upvotes: 2
Views: 1322
Reputation: 83
I know I am late but I just ran into the same problem and
since launchFragmentInContainer starts the fragment in an instance of EmptyFragmentActivity I tried the following and it worked for me
@get:Rule
val intentsTestRule = IntentsTestRule(FragmentScenario.EmptyFragmentActivity::class.java)
hope that helps
Upvotes: 3