android – 使用Espresso Stubbing Intent时出错

我有两个应用程序通过意图相互交互.我想验证一下,假设App A正确调用了App B的startActivity而没有实际启动App B.我尝试了各种意图组合,Espresso仍然通过意图启动App B而不是简单地将其删除.这会导致其他测试失败,因为应用程序B阻止了UI.任何想法?
@RunWith( AndroidJUnit4.class )
@LargeTest
public class MyActivityUiIntentsTest
{

    @Rule
    public IntentsTestRule<MyActivity> activityRule =
            new IntentsTestRule<>( MyActivity.class,true,false );

    @Test
    public void shouldStartOtherActivityWhenButtonClicked ()
    {
        Intents.init();
        intending( toPackage( "my.package" ) )
            .respondWith( new ActivityResult( Activity.RESULT_OK,null ) );

        activityRule.launchActivity( new Intent() );

        onView( withId( R.id.viewId ) ).perform( click() );
        intended( hasComponent( hasShortClassName( "the.other.class.name" ) ) );

        Intents.release();
    }
}

更新:onClick的代码

@OnClick( R.id.viewId )
public void startOtherActivity ()
{
   Intent intent = new Intent();
   intent.setClassName( "my.package","the.other.class.name" );
   startActivity( intent );
   finish();
}

解决方法

将你想要的代码移到launchActivity下面并移除.init()因为IntentsTestRule会在活动启动后为你调用init

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...