Android单元测试 - startActivity()崩溃app / test

时间:2016-03-09 12:53:45

标签: android unit-testing testing android-espresso

我正在使用Espresso& amp;测试Android应用Mockito并延伸ActivityInstrumentationTestCase2 UI测试工作得很好,但我按下它时按钮,onClick方法上的代码,触发startActivity(Intent),然后崩溃应用程序和测试。

在真实的Activity上我有这个onClick方法

    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);
    }

在测试类中我使用

调用该方法
onView(withId(R.id.btn_continue)).perform(click());

一旦startActivity触发,我会得到很多这样的行:

I/MonitoringInstrumentation: Unstopped activity count: 1

在日志上,然后应用程序崩溃

崩溃:

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

2 个答案:

答案 0 :(得分:1)

所以,回答我自己的问题。 如果有人需要这个信息。 没有办法捕获Context.startActivity(); Activity将启动,为防止崩溃,您需要模拟新Activity中的所有方法。

答案 1 :(得分:1)

你可以像使用ACTION_IMAGE_CAPTURE意图演示的official samples一样存根意图:

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

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

在你的情况下可能是这样的:

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

this tutorial对你来说可能很有意思