Android JUnit: assert inside the handler

626 views Asked by At

I am trying make a assert inside of handler but without success. I tried execute it with threads, runnables, etc. Next code is one example:

public class ServerTest extends AndroidTestCase {
    private final String TAG = this.getClass().getSimpleName(); 
    private final int ACK = 1;

    public void testSendToServer(){
        final ServerStub server = new ServerStub();         
        final CountDownLatch signal = new CountDownLatch(1);        

        new Thread(new Runnable() {             
            Handler handler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    // This log never appear
                    Log.w(TAG, "handleMessage(Message "+msg+")");
                    assertEquals(ACK, msg.what);

                    signal.countDown();
                    return true;
                }
            });

            @Override
            public void run() {
                // This log never appear
                Log.w(TAG, "sentToServer()");
                server.sentToServer(handler);               
            }
        }).start(); 

        try {
            signal.await(15, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }           
        //This assert fails
        assertEquals(0, signal.getCount());
    }

    class ServerStub{
        private final String TAG = this.getClass().getSimpleName(); 

        public void sentToServer(Handler handler){
            Message message = new Message();
            message.what = ACK;

            Log.i(TAG, "ACK_RECEIVED");
            handler.sendMessage(message);
        }
    }
}

I want to test a method class that doesn't return nothing, just "do something" and return the response using a handler.

I execute the test case testSendToServer() with the next intention:

In this test a ServerStub is created that has a method sentToServer(Handler handler). In this method simply send an "ACK" message to the handler. After that I create a CountDownLatch signal = new CountDownLatch(1); to call signal.countDown(); inside the handler handleMessage(Message msg) method to know if it is executed.

Continue with the test, I create a new Thread where a Handler is created and sentToServer(Handler handler) is called. Inside the Handler a assertEquals(ACK, msg.what); is called too where I need to know if the ServerStub sent me the "ACK".

Main Thread is waiting for 15 seconds or the signal.countDown(); is executed. Finally call assertEquals(0, signal.getCount()); to check if Handler was executed.

However the handler handleMessage(Message msg) method is never executed.

I read other posts and I tried a lot of examples, but I never found the answer. Is it possible something like that?

More information: Now I am trying create a handler inside a Thread, like this:

new Thread() {
        @Override
        public void run() {
            Log.w(TAG, "Thread run");
            Handler handler = new Handler(new Handler.Callback() {
                ...
            }
        }
    }.start();

The log "Thread run" is printed but after that I get a "Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'".

0

There are 0 answers