GetActivity () method blocks unlimited time during unit testing

I am trying to test two different Activity classes, where one Activity calls another one. Here is my code, and then I will explain the problem:

IntroActivityTest

 public class IntroActivityTest extends ActivityInstrumentationTestCase2<IntroActivity> { IntroActivity activity; public IntroActivityTest() { super( IntroActivity.class ); } @Override protected void setUp() throws Exception { super.setUp(); activity = getActivity(); } public void testIntroBypass() { if ( new SharedPreferencesHelper( getInstrumentation().getTargetContext() ).retrieveUserToken() == null ) { assertTrue( !activity.isFinishing() ); } else { assertTrue( activity.isFinishing() ); } } } 

RootActivityTest:

 public class RootActivityTest extends ActivityInstrumentationTestCase2<RootActivity> { RootActivity activity; public RootActivityTest() { super( RootActivity.class ); } @Override protected void setUp() throws Exception { super.setUp(); activity = getActivity(); } public void testInitialTab() { assertTrue( activity.getSupportActionBar().getSelectedTab().getText().toString().equalsIgnoreCase( "Library" ) ); } } 

In IntroActivityTest , if the user token from SharedPreferences not null, it immediately starts RootActivity . If it is zero, it stays on IntroActivity . The problem is that if it is not equal to zero, the first test passes ( IntroActivityTest ), and then it hangs when the getActivity() method is getActivity() in RootActivityTest , and the test just hangs ... no exceptions, it just hangs on this line. If the user token is zero, it performs both tests perfectly.

What could be the reason for this? From the observation, it seems that RootActivityTest trying to use RootActivity , which was launched from IntroActivity , but should it not run its own instance of RootActivity ?

+6
source share
2 answers

According to the ActivityInstrumentationTestCase2 API :

This class provides functional testing of a single action. Testing will be created using the system infrastructure (by calling InstrumentationTestCase.launchActivity ()), and you can directly manipulate your activities.

Each implementation of ActivityInstrumentationTestCase2 is completely isolated and has its own life cycle, which is independent of another implementation of ActivityInstrumentationTestCase2. The activity being tested should always be created through the infrastructure of the measuring equipment, and not from the test itself. In your case, RootActivityTest will not pick up the RootActivity launched by IntroActivity from the application and the continuous test to check RootActivity. If there is a RootActivity that comes from no where (and not using InstrumentationTestRunner) and starts from the front, when RootActivityTest InstrumentationTestRunner starts up, it gets confused when trying to create a verified RootActivity and just stops and waits until this stranger is killed.

To check what you want i.e. if the user token from SharedPreferences is not null, it immediately starts RootActivity. If it is zero, it remains included in IntroActivity , you can write everything in IntroActivityTest and use Instrumentation.ActivityMonitor to identify the RootActivity started with IntroActivity. Check here for sample code. note that you need to complete RootActivity after completing testing in IntroActivityTest so that RootActivity can start correctly when getActivity () is called in RootActivityTest.

Use RootActivityTest to test everything related to RootActivity after it has been launched and brought to the forefront, for example, the TextView is well displayed, the click button does the right thing, etc. At RootActivityTest, you don’t have to worry about where and how RootActivity will start, just call getActivity () and ask for the toolkit for the RootActivity to be tested.

+7
source

I ran into the same problem when the previous test did not close the activity and the new test started with intent. But the Android OS sees that the activity is already running and has done nothing, so the InstrumentationTestCase began to wait for the activity that has already been sent

0
source

Source: https://habr.com/ru/post/921989/


All Articles