Tuesday, October 11, 2011

[android-developers] Unit testing methods containing Handler

Hi !
I have a problem.
I have a class MyObject that has a method start() :

public class MyObject {

public void start() {
Handler h = new Handler();
new Thread() {
@Override
public void run() {
super.run();

h.post(new Runnable() {
@Override
public void run() {
mIsRunning = true;
onStart();
}
});

}
}.start();
}

}

I'm trying to test that onStart() callback is called.
I'm using AndroidTestCase class, and my test looks like this :

@UiThreadTest
public void testOnStartIsCalled() {
mMyObject.start();
assertTrue(mMyObject.isRunning());
}

But the Runnable posted in the Handler has never ran.
How could I test that the callback has been called ?

Thanks a lot, I'm a little bit hopeless...

Regards

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment