2

I'm testing Android app using Espresso & Mockito and extending ActivityInstrumentationTestCase2 The UI test works great but i have a button the when pressing on it, the code on the onClick method, fires startActivity(Intent), and than crash the app and test.

On the real Activity I have this onClick method

    public void onContinueButtonClicked(View v)
    {
        Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

and on the test class i call that method using

onView(withId(R.id.btn_continue)).perform(click());

once the startActivity fires, I get a lot of those lines:

I/MonitoringInstrumentation: Unstopped activity count: 1

on the log and then the app crashed

the crash :

03-09 14:39:41.654 21469-21505/com.example I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:41.704 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:41.754 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:41.804 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:41.854 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:41.905 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:41.955 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:42.005 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:42.055 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:42.105 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:42.156 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:42.206 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:42.256 21469-21505/com.example  I/MonitoringInstrumentation: Unstopped activity count: 1
03-09 14:39:42.314 21469-21505/com.example  E/THREAD_STATE:   Thread[main,5,main]
                                                                       android.os.MessageQueue.nativePollOnce(Native Method)
                                                                       android.os.MessageQueue.next(MessageQueue.java:323)
                                                                       android.os.Looper.loop(Looper.java:135)
                                                                       android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       java.lang.reflect.Method.invoke(Native Method)
                                                                       com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

                                                                     Thread[Thread-904,5,main]
                                                                       java.lang.Object.wait(Native Method)
                                                                       java.lang.Thread.parkFor$(Thread.java:1220)
                                                                       sun.misc.Unsafe.park(Unsafe.java:299)
                                                                       java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
                                                                       java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2013)
                                                                       java.util.concurrent.PriorityBlockingQueue.take(PriorityBlockingQueue.java:510)
                                                                       com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:92)

                                                                     Thread[FinalizerDaemon,5,system]
                                                                       java.lang.Object.wait(Native Method)
                                                                       java.lang.Object.wait(Object.java:423)
                                                                       java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:101)
                                                                       java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:72)
                                                                       java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
                                                                       java.lang.Thread.run(Thread.java:818)

                                                                     Thread[Queue,10,main]
                                                                       java.lang.Object.wait(Native Method)
                                                                       java.lang.Thread.parkFor$(Thread.java:1220)
                                                                       sun.misc.Unsafe.park(Unsafe.java:299)
                                                                       java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
                                                                       java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2013)
                                                                       java.util.concurrent.PriorityBlockingQueue.take(PriorityBlockingQueue.java:510)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.performOperation(DependencyPriorityBlockingQueue.java:197)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.get(DependencyPriorityBlockingQueue.java:236)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.take(DependencyPriorityBlockingQueue.java:65)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.take(DependencyPriorityBlockingQueue.java:46)
                                                                       java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1038)
                                                                       java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1098)
                                                                       java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                       java.lang.Thread.run(Thread.java:818)

                                                                     Thread[Queue,10,main]
                                                                       java.lang.Object.wait(Native Method)
                                                                       java.lang.Thread.parkFor$(Thread.java:1220)
                                                                       sun.misc.Unsafe.park(Unsafe.java:299)
                                                                       java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
                                                                       java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2013)
                                                                       java.util.concurrent.PriorityBlockingQueue.take(PriorityBlockingQueue.java:510)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.performOperation(DependencyPriorityBlockingQueue.java:197)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.get(DependencyPriorityBlockingQueue.java:236)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.take(DependencyPriorityBlockingQueue.java:65)
                                                                       io.fabric.sdk.android.services.concurrency.DependencyPriorityBlockingQueue.take(DependencyPriorityBlockingQueue.java:46)
                                                                       java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1038)
                                                                       java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1098)
                                                                       java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                       java.lang.Thread.run(Thread.java:818)

                                                                     Thread[Queue,10,main]
                                                                       java.lang.Object.wait(Native Method)
                                                                       java.lang.Thread.parkFor$(Thread.java:1220)
                                                                       sun.misc.Unsafe.park(Unsafe.java:299)
                                                                       java.util.concurrent.locks.LockSupport.park(LockSupport.java:158)
                                                                       java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObjec

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'rx.Subscription rx.Observable.subscribe(rx.functions.Action1, rx.functions.Action1)' on a null object reference
at com.example.activities.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:532)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
... 23 more
1
  • 1
    did you check this line MainActivity.java:52 Commented Mar 14, 2016 at 14:02

2 Answers 2

1

So, Answering my own question. if anyone will need this info. There is no way to catch the Context.startActivity(); The Activity will start, to prevent crashed, you need to mock all the methods in the new Activity.

Sign up to request clarification or add additional context in comments.

Comments

1

You could stub the intent like the official samples demonstrated with the ACTION_IMAGE_CAPTURE intent:

@Before
public void stubCameraIntent() {
    ActivityResult result = createImageCaptureActivityResultStub();

    // Stub the Intent.
    intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
}

In your case that could be something like this:

@Before
public void stubNextIntent() {
    intended(hasComponent(NextActivity.class.getName()));
}

this tutorial might be interesting for you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.