RobotiumTech / robotium

Android UI Testing
http://www.robotium.org
Apache License 2.0
2.86k stars 786 forks source link

ActivityMonitor.waitForActivity() inside setupActivityStackListener() not working when running high number of test cases. #793

Closed manirajaa2 closed 8 years ago

manirajaa2 commented 9 years ago

Hi, I am using the latest robotium and i am getting a null pointer exception as follow .

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity android.app.Instrumentation$ActivityMonitor.waitForActivity()' on a null object reference

only when running test case more that 300 . If i split the job then it is working fine however i am in need to run large amount of test cases.

renas commented 9 years ago

Thanks for reporting this. Can you please send me the complete stack trace? On Nov 6, 2015 4:08 AM, "manirajaa2" notifications@github.com wrote:

Hi, I am using the latest robotium and i am getting a null pointer exception as follow .

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity android.app.Instrumentation$ActivityMonitor.waitForActivity()' on a null object reference

only when running test case more that 300 . If i split the job then it is working fine however i am in need to run large amount of test cases.

— Reply to this email directly or view it on GitHub https://github.com/RobotiumTech/robotium/issues/793.

manirajaa2 commented 9 years ago

Actually while running the tests following is the only stack trace which i am getting in the console. There is no stack trace followed with app code or testing code .

java.lang.NullPointerException at com.robotium.solo.ActivityUtils$1.run(ActivityUtils.java:152) at java.lang.Thread.run(Thread.java:841)

scottlahay commented 8 years ago

I'm getting the same issue. Test runs fine by itself but crashes when I run 2 or more test case.

E/AndroidRuntime: FATAL EXCEPTION: activityMonitorThread
Process: com.getintheloop.getintheloop.dev, PID: 4126
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity     android.app.Instrumentation$ActivityMonitor.waitForActivityWithTimeout(long)' on a null object reference
at com.robotium.solo.ActivityUtils$1.run(ActivityUtils.java:152)
at java.lang.Thread.run(Thread.java:818)
amvasilyev commented 8 years ago

Hello.

I am getting the same error trying to run Robotium tests using JUnit4 runner. One test runs fine, but during the execution of the second test, the application falls with the NullPointerException.

My test are created for a simple case of a list of records having the editor screen. The first test (usually addition of a new record) runs fine, but the second one (the modification of the record) breaks after the dialog screen is closed. The test is able to click on the main screen to invoke the dialog, but when it is closed, the test fails.

The application is written using mortar/flow, so there is a single activity application and minimal amount of library-driven fragments. The editor is a custom flow screen. There are also tests that were written using the old instrumentation (based on JUnit 3) and Robotium - they are running fine.

build.gradle:

androidTestCompile 'com.jayway.android.robotium:robotium:5.5.3'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.3'
androidTestCompile('com.android.support.test:rules:0.4.1')
androidTestCompile('com.android.support.test:runner:0.4.1')
// This dependency is needed due to runner 0.4.1 depending on 23.0.1
androidTestCompile 'com.android.support:support-annotations:23.1.1'

The structure of the tests is the following. Custom methods are simple private methods that interact with the corresponding view using Robotium.Solo.

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import com.robotium.solo.Solo;
....
@RunWith(AndroidJUnit4.class)
@LargeTest
public class EditorTest {

    @Rule
    public ActivityTestRule<MainActivity> activityTestRule =
            new ActivityTestRule<>(MainActivity.class);

    private Solo solo;

    @Before
    public void clearDatabase() {
       // Database clear code was removed
        Log.d("test", activityTestRule.getActivity().toString());
        solo = new Solo(InstrumentationRegistry.getInstrumentation(),
                activityTestRule.getActivity());
    }

    @Test
    public void newRecordAddition() {
        Record addedRecord = addSleepRecord();
        assertRecordIsDisplayedInRecyclerView(addedRecord);
    }

    @Test
    public void recordModification() {
        addSleepRecord();
        solo.clickInRecyclerView(0);
        Record modifiedRecord = new SleepTime(...);
        fillInRecordEditScreenAndSave(modifiedRecord);
        assertRecordIsDisplayedInRecyclerView(modifiedRecord);
    }

The relevant output of the logcat:

01-30 10:26:07.082 9695-9709/? I/TestRunner: started: newRecordAddition(my.app.RecordEditorTest)
01-30 10:26:08.031 9695-9709/my.app D/test: my.app.core.MainActivity@64244d8
01-30 10:26:16.030 9695-9709/my.app I/TestRunner: finished: newRecordAddition(my.app.RecordEditorTest)
01-30 10:26:16.034 9695-9709/my.app I/TestRunner: started: recordRemoval(my.app.RecordEditorTest)
01-30 10:26:16.548 9695-9709/my.app D/test: my.app.core.MainActivity@b7c7871

... A lot of records like this: 
01-30 10:26:18.569 9695-9695/my.app W/art: Attempt to remove non-JNI local reference, dumping thread
01-30 10:26:18.948 9695-9695/my.app W/AsyncHttpClient: Passed contentType will be ignored because HttpEntity sets content type
01-30 10:26:19.122 9695-9786/my.app E/AndroidRuntime: FATAL EXCEPTION: activityMonitorThread
  Process: my.app, PID: 9695
  java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity 
  android.app.Instrumentation$ActivityMonitor.waitForActivityWithTimeout(long)'
  on a null object reference
  at
  com.robotium.solo.ActivityUtils$1.run(ActivityUtils.java:152)
    at java.lang.Thread.run(Thread.java:818)
amvasilyev commented 8 years ago

My issue was resolved when I have switched from emulator as a target platform to a real device.

renas commented 8 years ago

Thanks for reporting this. The issue will be fixed shortly.

amvasilyev commented 8 years ago

Hello.

I have continued to add more tests to the project. There are 4 of them now. And today I found a new lead in logcat data. Before every crash, there is a line denoting that the background GC has finished.

Also before the second test there is a line that states that there are activities that are to be destroyed. And there is a notice about that action just before the crash.

The corresponding part of the logcat data is the following:

02-02 18:48:24.266 29326-29359/? I/TestRunner: run started: 4 tests
02-02 18:48:24.280 29326-29359/? I/TestRunner: started: sleepRecordAddition(my.app.HistoryScreenTests)
02-02 18:48:24.283 29326-29326/? I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 0
02-02 18:48:24.284 29326-29359/? D/ActivityInstrumentationRule: Launching activity my.app.core.MainActivity

LINES REMOVED FROM THE FIRST TEST RUN

02-02 18:48:35.455 29326-29359/my.app I/TestRunner: finished: newRecordAddition(my.app.HistoryScreenTests)
02-02 18:48:35.463 29326-29326/my.app I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 1
02-02 18:48:35.469 29326-29359/my.app I/TestRunner: started: recordRemoval(my.app.HistoryScreenTests)
02-02 18:48:35.471 29326-29326/my.app I/MonitoringInstrumentation: Activities that are still in CREATED to STOPPED: 1
02-02 18:48:35.471 29326-29359/my.app D/ActivityInstrumentationRule: Launching activity my.app.core.MainActivity
02-02 18:48:35.476 29326-29638/my.app D/MonitoringInstrumentation: execStartActivity(context, ibinder, ibinder, activity, intent, int, bundle
02-02 18:48:35.476 29326-29638/my.app I/Timeline: Timeline: Activity_launch_request id:my.app time:7315962
02-02 18:48:35.552 29326-29326/my.app D/LifecycleMonitor: Lifecycle status change: my.app.core.MainActivity@83387af in: PRE_ON_CREATE
02-02 18:48:35.586 29326-29326/my.app I/Ads: Starting ad request.
02-02 18:48:35.587 29326-29326/my.app I/Ads: Use AdRequest.Builder.addTestDevice("...") to get test ads on this device.
02-02 18:48:35.619 29326-29326/my.app D/LifecycleMonitor: Lifecycle status change: my.app.core.MainActivity@83387af in: CREATED
02-02 18:48:35.625 29326-29326/my.app W/AsyncHttpClient: Passed contentType will be ignored because HttpEntity sets content type
02-02 18:48:35.626 29326-29326/my.app D/LifecycleMonitor: Lifecycle status change: my.app.core.MainActivity@83387af in: STARTED
02-02 18:48:35.627 29326-29326/my.app D/LifecycleMonitor: Lifecycle status change: my.app.core.MainActivity@83387af in: RESUMED
02-02 18:48:35.720 29326-29339/my.app I/art: Background sticky concurrent mark sweep GC freed 10316(510KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 16MB/16MB, paused 5.523ms total 37.170ms
02-02 18:48:35.730 29326-29341/my.app W/Ads: There was a problem getting an ad response. ErrorCode: 2
02-02 18:48:35.829 29326-29326/my.app V/JsonHttpResponseHandler: response body is null, calling onFailure(Throwable, JSONObject)
02-02 18:48:35.860 29326-29326/my.app I/Ads: Scheduling ad refresh 60000 milliseconds from now.
02-02 18:48:35.860 29326-29326/my.app W/Ads: Failed to load ad: 2
02-02 18:48:35.899 29326-29326/my.app I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@33dbb86d time:7316384
02-02 18:48:36.328 29326-29326/my.app D/LifecycleMonitor: Lifecycle status change: my.app.core.MainActivity@14675404 in: STOPPED
02-02 18:48:36.329 29326-29326/my.app D/LifecycleMonitor: Lifecycle status change: my.app.core.MainActivity@14675404 in: DESTROYED
02-02 18:48:37.758 29326-29339/my.app I/art: Background partial concurrent mark sweep GC freed 7369(380KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 16MB/21MB, paused 1.251ms total 104.278ms
02-02 18:48:41.825 29326-29505/my.app E/AndroidRuntime: FATAL EXCEPTION: activityMonitorThread
                                                                                 Process: my.app, PID: 29326
                                                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Activity android.app.Instrumentation$ActivityMonitor.waitForActivityWithTimeout(long)' on a null object reference
                                                                                     at com.robotium.solo.ActivityUtils$1.run(ActivityUtils.java:152)
                                                                                     at java.lang.Thread.run(Thread.java:818)
renas commented 8 years ago

Can you please try to reproduce this issue with the attached jar? robotium-solo-5.5.4-SNAPSHOT.jar.zip

amvasilyev commented 8 years ago

With this version I was able not only to run all tests on a real device, but also on the emulator. As I have said earlier with 5.5.3 the second test always broke the execution, but now all of them are passing.

Thank you for fixing this!

renas commented 8 years ago

Thanks again for reporting this. This has been fixed in Robotium 5.5.4.

On Tue, Feb 2, 2016 at 9:08 PM, Andrey Vasilev notifications@github.com wrote:

With this version I was able not only to run all tests on a real device, but also on the emulator. As I have said earlier with 5.5.3 the second test always broke the execution, but now all of them are passing.

Thank you for fixing this!

— Reply to this email directly or view it on GitHub https://github.com/RobotiumTech/robotium/issues/793#issuecomment-179014160 .

scottlahay commented 8 years ago

Just got back to this. I know Its a little late for feedback, but the fix works great with my tests. Love the work you guys are doing. Rock on.

manirajaa2 commented 8 years ago

After this version of fix 5.5.4 UI thread is hanging a lot and leads Key dispatch Timeout issue in the app and leads to ANR Issue. Please resolve it.

renas commented 8 years ago

@manirajaa2 would it be possible for you to share an app that exhibits this issue?