Otis Lee Otis Lee - 9 days ago 7
Android Question

Android Espresso-Intents can't use in Fragment while startActivityForResults

I'm using Espresso-Intents to testing my Fragment while startActivityForResults.

Below Android L, It works fine! But above Android M, it seems can't verify the Intent is correct or wrong.

However, I try with

getActivity().startActivityForResult()

, It works fine!

But I don't want to use this solution.

Here is my code:

view.findViewById(R.id.btnStartActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent it = new Intent(getActivity(), SecondActivity.class);
MainFragment.this.startActivityForResult(it, 0);
//It through test
//getActivity().startActivityForResult(it, 0);
}
});


And here is my test case:

@Test
public void testCase02() throws Exception {
mActivityTestRule.launchActivity(new Intent());

Intents.intending(IntentMatchers.hasComponent(SecondActivity.class.getName())).respondWith(new Instrumentation.ActivityResult(0, new Intent()));

onView(allOf(withId(R.id.btnStartActivity),
withText("Start Activity From Fragment"))).perform(click());

Intents.intended(IntentMatchers.hasComponent(SecondActivity.class.getName()));
}


If you want see more specific, here is my github:

https://github.com/harriswan9a/android-testing

Is there any solution?

Here is my error log:



android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: has component: has component with: class name: is "com.dev.lib.testespressointents.SecondActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String

Matched intents:[]

Recorded intents:
-Intent { flg=0x14000000 cmp=com.dev.lib.testespressointents/.MainActivity } handling packages:[[com.dev.lib.testespressointents]])
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at android.support.test.espresso.intent.Intents.intended(Intents.java:187)
at android.support.test.espresso.intent.Intents.intended(Intents.java:169)
at com.dev.lib.testespressointents.MainActivityTest.testCase02(MainActivityTest.java:82)
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 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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$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.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)
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: has component: has component with: class name: is "com.dev.lib.testespressointents.SecondActivity" package name: an instance of java.lang.String short class name: an instance of java.lang.String

Matched intents:[]

Recorded intents:
-Intent { flg=0x14000000 cmp=com.dev.lib.testespressointents/.MainActivity } handling packages:[[com.dev.lib.testespressointents]])
at junit.framework.Assert.fail(Assert.java:50)
at android.support.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:87)
at android.support.test.espresso.intent.Intents.internalIntended(Intents.java:282)
at android.support.test.espresso.intent.Intents$2.check(Intents.java:190)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Tests ran to completion.





Actually, It's my problem. I just use my friend's account. thanks all

Answer

The construction of intended method is probably wrong.

Check such one:

Intents.intended(IntentMatchers.hasComponent(new ComponentName(getTargetContext(), SecondActivity.class)));

Dependency in build.gradle:

com.android.support.test.espresso:espresso-intents:2.2.2