Search code examples
androidandroid-intentmvpandroid-testingandroid-instrumentation

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.


Solution

  • 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.