I have two applications that interact with each other through intents. I would like to verify that let's say App A correctly calls the startActivity
for App B without actually launching App B. I have tried various combinations of intending
and Espresso still launches App B through the intent instead of just stubbing it out. This causes the remaining tests to fail as the UI is blocked by App B. Any ideas?
@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();
}
}
UPDATED: Code for the onClick
:
@OnClick( R.id.viewId )
public void startOtherActivity ()
{
Intent intent = new Intent();
intent.setClassName( "my.package", "the.other.class.name" );
startActivity( intent );
finish();
}
content. intent' is deprecated. I found some threads regarding the usage of getIntent() but I am creating a new intent in my case in a RecyclerViewAdapter to open a new Activity.
Android Intent Tutorial. Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc. The dictionary meaning of intent is intention or purpose.
An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.
Move your intending...
code below launchActivity and remove .init()
because the IntentsTestRule
will call init for you after the activity is launched
One of the possible solutions is to have indirection for intent dispatching.
For example we have IntentDispatcher
which we replace with test implementation in functional ui tests via trick with custom instrumentation test runner.
Real implementation of IntentDispatcher
just calls context.startActivity()
while in tests we open special activity that displays all content of the Intent
so we're able to verify that it was Intent
that we wanted to process with Espresso matchers.
Also we wrote bunch of rules to handle things like opening camera app and mocking the result or just mocking regular startActivity()
calls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With