Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run code in the UI thread in an Android JUnit test without an 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() 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?

like image 426
The Science Boy Avatar asked Apr 26 '11 13:04

The Science Boy


1 Answers

Android.OS.Handler should be able to update the ui.

To use a handler you have to subclass it and overide handleMessage() to process messages

Update:

I am using monodroid, so I dont know if this is completely translatable, But,

You may be able to use

new Handler(context.getMainLooper()).post(runnable);
like image 121
The Lazy Coder Avatar answered Oct 16 '22 14:10

The Lazy Coder