startActivityForResults

时间:2016-11-25 10:00:13

标签: android android-fragments android-intent android-espresso

我正在使用Espresso-Intents测试我的Fragment而startActivityForResults。

Android L以下,它运行正常!但是在Android M之上,似乎无法验证Intent是正确还是错误。

但是,我尝试使用getActivity().startActivityForResult() ,它工作正常!

但我不想使用这个解决方案。

这是我的代码:

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); 
        }
    });

这是我的测试用例:

@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()));
}

如果你想看到更具体的,这是我的github:

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

有没有解决方案?

这是我的错误日志:

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.

实际上,这是我的问题。我只是用我朋友的账号。谢谢所有

1 个答案:

答案 0 :(得分:0)

构建intended方法可能不对。

检查一下:

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

build.gradle中的依赖关系:

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