I used ActivityInstrumentationTestCase2 to check activity with ExoPlayer and fix resources. Since clean and final checks are the same for all tests, I thought tearDown () was a good place to implement them. All tests run separately without any problems, but when I run several tests, sometimes getActivity () did not return. My tearDown () implemented various things:
- check activity status (various assert () calls)
- check player status (various assert () calls)
- manually clear player resources (calling close () and release ())
- setActivity (null) (this caused the problem)
- super.tearDown ()
I tried all the suggested workarounds, such as overriding getActivity () and using other toolkit methods to create and clean up the activity. These methods did not help.
And a lot of debugging showed that using the onDestory () script above, the activity of the previous test can overlap with onCreate () for the next test to work. Thus, the logs displayed life cycle events in the following order:
test1.getActivity(); test1.tearDown() called; test1.tearDown() over; test2.getActivity() test2.onCreate(); test1.onStop(); --> why is this late? test1.onDestroy(); --> why is this late? test2.tearDown() called; test2.tearDown() over; test3.getActiviy() --> this should call test3.onCreate, but did not and never returned.
This happens even when test cases are implemented in separate classes / files of ActivityInstrumentationTestCase2. And the first time this overlap does not cause problems, so 2 tests are always in order, but executing 3 tests in any order that lead to this overlap causes the 3rd call to getActivity () to never return.
I tried everything like calling onPause () + onStop () + onDestroy () manually using the toolkit, representing very long waiting periods between tests, enhancing the cleaning of the TestCase toolkit activity, reordering the checks of my tearDown, but nothing helped. Finally, I accidentally deleted setActivity (null) before my checks, and the life cycle events got the correct ordering:
test1.tearDown() called; test1.onStop(); test1.onDestroy(); test1.tearDown() over; test2.getActivity() test2.onCreate(); ...
So what really mattered in my case: don't call ActivityTestCase.setActivity (). This leads to the fact that super.tearDown () does not directly trigger the activity lifecycle events, cleaning will happen later and cause problems.