Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test handler message using roboelectric or mockito

I'm trying to test a piece of code using roboelectric and mockito framework that contains a handler.sendMessage(msg):

public void emailLogin(){
...
    mHandler.sendMessage(message);
}

Handler mHandler = new Handler(new Callback() {

        public boolean handleMessage(Message msg) {
            System.out.println("called");
            return false;
        }
});

The handler works perfectly without normal envoirnment, but if I write a unit test for that method, the handler is not triggered.

Is the handler.sendMessage(msg) missing from robolectric? Or We have to test handler seperately?

I don't want to test the handler it self through a unit test, what I want to test is my function, that contains the handler.

like image 804
Mukesh Y Avatar asked Nov 26 '25 05:11

Mukesh Y


1 Answers

Take a look into:

Robolectric.runUiThreadTasks();

I was running into the same problem with Handlers inside the loopj Android Asynchronous Http Client library not getting hit when running tests with using the RobolectricTestRunner class, and ended up using similar approach that was mentioned here: https://groups.google.com/forum/#!topic/robolectric/hbcTw3jLXys

like image 61
chinabuffet Avatar answered Nov 27 '25 21:11

chinabuffet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!