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.