I'm learning android instrumentation testing with espresso. I have an app which has a drawer menu and there is a menu called About. I was testing click on that menu item and contents of activity.
testfunction:
@Test public void testNavigationDrawerAboutMenu() { onView(withId(R.id.drawer_layout)) .perform(DrawerActions.open()); //open drawer onView(withText("About")).perform(click()); onView(withId(R.id.aboutsptemail)).check(matches(withText(R.string.screen_about_support_email))); onView(withId(R.id.aboutcpright)).check(matches(isDisplayed())); onView(withId(R.id.aboutprivacy)).check(matches(isDisplayed())); onView(withId(R.id.abouttermsconditions)).check(matches(isDisplayed())); onView(withId(R.id.aboutsptemail)).perform(click()); }
now the last textview has weblink embedded in it. so when you click on it, it opens the link(www.support.com) in a web view within the app. I want to test this functionality. so I tried this:
intended(hasComponent(WebViewActivity.class.getName())); //check if webview called on supportEmail link click
but test fails with this error trace:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.test.espresso.intent.OngoingStubbing android.support.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference at android.support.test.espresso.intent.Intents.intending(Intents.java:155) at com.ScanBuy.SmartLabel.NavigationDrawerActivityTests.testNavigationDrawerAboutMenu(NavigationDrawerActivityTests.java:94) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
I have also tried resolve by idling the resources before checking for intent. But didn't work. Can anybody help?
The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app.
Espresso is an Android test framework developed by Google for UI testing. Espresso automatically synchronizes test actions with the user interface of the mobile app and ensures that activity is started well before the actual test run.
I had the same problem and solved it by using IntentsTestRule
instead of ActivityTestRule
. IntentsTestRule
is a subclass of ActivityTestRule
. Set up your @Rule
which creates the activity like so:
@Rule public IntentsTestRule<MyActivity> mActivity = new IntentsTestRule<MyActivity>(MyActivity.class) { @Override protected Intent getActivityIntent() { ... } };
See the following project for more information: https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample
If you are using a custom ActivityTestRule
, you can add the proper Intents.init(), Intents.release()
calls:
@Override protected void afterActivityLaunched() { Intents.init(); super.afterActivityLaunched(); } @Override protected void afterActivityFinished() { super.afterActivityFinished(); Intents.release(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With