在没有Activity的情况下,如何在Android JUnit测试中的UI线程中运行代码? [英] How do I run code in the UI thread in an Android JUnit test without an Activity?
问题描述
我有一个依赖于AsyncTask
的数据库组件来检索数据.一旦在应用程序中使用过,它将始终从UI线程中调用,但是我如何在JUnit测试中做到这一点而无需实例化Activity
(我只是没有任何活动,也不必为它创建一个活动.测试目的)?
I have a database component that relies on AsyncTask
to retrieve data. Once used in the application it will always be called from the UI thread, but how do I do that in the JUnit tests without instantiating an Activity
(i just don't have an activity, nor should I have to create one just for testing purposes)?
runOnUiThread()
是一种活动方法,因此不是一种选择.是否可以通过从Android测试库中获取一个UI线程或通过实现MockUiThread来模拟UI线程?
runOnUiThread()
is an activity method so it's not an option. Is there a way to simulate the UI thread either by getting one from the Android test library or by implementing a MockUiThread?
推荐答案
Android.OS.Handler应该能够更新ui.
Android.OS.Handler should be able to update the ui.
要使用处理程序,您必须将其子类化并覆盖handleMessage()来处理消息
To use a handler you have to subclass it and overide handleMessage() to process messages
更新:
我使用的是Monodroid,所以我不知道这是否完全可翻译,但是,
I am using monodroid, so I dont know if this is completely translatable, But,
您也许可以使用
new Handler(context.getMainLooper()).post(runnable);
这篇关于在没有Activity的情况下,如何在Android JUnit测试中的UI线程中运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!