Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error When Stubbing Intent with Espresso

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();
}
like image 874
brwngrldev Avatar asked Apr 20 '16 19:04

brwngrldev


People also ask

Is intent deprecated?

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.

What is intent in android studio?

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.

What is an intent in Java?

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.


2 Answers

Move your intending... code below launchActivity and remove .init() because the IntentsTestRule will call init for you after the activity is launched

like image 147
Nick Korostelev Avatar answered Sep 17 '22 18:09

Nick Korostelev


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.

like image 22
Artem Zinnatullin Avatar answered Sep 17 '22 18:09

Artem Zinnatullin