简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM