Mohammad Alfaz
Mohammad Alfaz

Reputation: 152

How to test second activity started in instrumentation test in MVP?

I am new to MVP in android.

My question is related to Instrumentation test in android. I am calling second_activity() in main_activity() using intent. So how can I test whether second_activity is called or not in instrumentation test using espresso.

I have successfully tested in unit test using junit and mockito.

Upvotes: 0

Views: 1308

Answers (1)

Mohammad Alfaz
Mohammad Alfaz

Reputation: 152

I solved it. Let me explain what I did.

First step: Place your intent into a method in main activity

public void gotoSecond() {

            Intent intent = new Intent(context, SecondActivity.class);
            startActivity(intent);
}

And then place this code in Instrumenation test class file.

private MainActivity mTestActivity;

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
        MainActivity.class);

public MainActivityTest() {
    super(MainActivity.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    mTestActivity = getActivity();
}

@Test
public void testSecond(){
   //calling activity method using getActivity()
    mTestActivity.gotoSecond();
}

hope this will help someone who needs.

Upvotes: 2

Related Questions