0

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mohammad Alfaz
  • 152
  • 1
  • 2
  • 13

1 Answers1

2

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.

Mohammad Alfaz
  • 152
  • 1
  • 2
  • 13