我有两个应用程序通过意图相互交互。我想验证一下,比如说应用程序A正确地调用了startActivity
,启动了应用程序B而不是实际地启动它。我已经尝试过各种组合的intending
和Espresso,但仍然会通过意图启动应用程序B而不是只是将其存根。这会导致剩余的测试失败,因为应用程序B阻塞了用户界面。有任何想法吗?
@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();
}
onClick
的示例代码。 - brwngrldevIntentsTestRule
会自动处理init
和release
。 - Blundell