Question

I was unit testing a simple app today. I have a method

protected void onRestart() {
    disp.setText("The numbers you entered were");
    super.onRestart();
}

And in my test case i'm using

public void testRestart(){
    String dispText = disp.getText().toString();
    getInstrumentation().callActivityOnStop(mActivity);
    assertEquals(dispText, disp.getText().toString());
}

The assertion returns true meaning that the text isn't changed. However when i use

public void testRestart(){
    String dispText = disp.getText().toString();
    getInstrumentation().callActivityOnRestart(mActivity);
    assertEquals(dispText, disp.getText().toString());
}

the assertion is false as expected.

According to the activity lifecycle onRestart() should always be called after onStop() if the user navigates away from the activity.

Shouldn't the onRestart() method be called after onStop() ? Or does calling getInstrumentation().callActivityOnStop(mActivity); kill the activity, instead of just stopping it ?

Was it helpful?

Solution

ActivityUnitTestCase is an isolated, unit test of a single Activity. The Activity Under Test does not participate of the system interactions.

You can start your Activity using startActivity(), and it will invoke onCreate(), however if you wish to further exercise Activity life cycle methods, you must call them yourself from your test case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top