我有两个应用程序通过意图相互交互.我想验证一下,假设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(); }