質問

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 ?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top