gpt4 book ai didi

Android 单元测试 - startActivity() 使应用程序/测试崩溃

转载 作者:可可西里 更新时间:2023-11-01 11:41:59 24 4
gpt4 key购买 nike

我正在使用 Espresso 和 Mockito 测试 Android 应用程序并扩展 ActivityInstrumentationTestCase2UI 测试效果很好,但我有一个按钮,当按下它时,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

最佳答案

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

关于Android 单元测试 - startActivity() 使应用程序/测试崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35892170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com