使用Espresso识别意图时出错

brw*_*dev 9 android android-intent android-espresso

我有两个应用程序通过意图相互交互.我想,以验证假设应用一个正确调用startActivity应用B没有实际启动应用B.我尝试了各种组合,intendingEspresso仍然通过意图启动App B,而不仅仅是将其删除.这会导致其余测试失败,因为App 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:代码onClick:

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

Nic*_*lev 4

将您的intending...代码移至 launchActivity 下方并删除,.init()因为IntentsTestRule在活动启动后,它将为您调用 init