androidunit-testingandroid-espressoandroid-testingandroid-instrumentation

How to convert Android SingleLaunchActivityTestCase to ActivityTestRule? (Instrumentation unit test)


The documentation for SingleLaunchActivityTestCase says that this class is now deprecated, and it should be replaced with ActivityScenarioRule or ActivityTestRule. But how is this done?

SingleLaunchActivityTestCase allowed an Activity to launch once and keep it open; then multiple tests can run during this time, then the Activity is closed. ActivityTestRule does not seem to have this functionality - it always relaunches the Activity for each @Test method.

So is it possible to make ActivityTestRule launch an Activity once and keep it open, and how do I ensure the context (from activityTestRule.getActivity()) is not null for each @Test function?

Example code here.


Solution

  • Use the constructor that does not start the activity by default (setting the launchActivity argument to false). Then use launchActivity() in your setup, but not in each test method. This way you will start it up once yourself, but each test will operate on the same instance.

    You'll probably want to also explicitly finish the activity at the end of the test class, for cleanup.

    Note: This isn't generally a best practice for testing though, since tests could depend on each other (which isn't a good idea), or provide incorrect results depending on the order they run, etc. since the activity state is persisted from one test to the next in this case.