Unit test for Runnable with Mockito

11,818

Pass a mock Handler to the constructor of TestClass.

Then use Mockito.verify() to assert that callHandler() method was called.

Involving concurrency

You can stub an answer that counts down on a CountDownLatch to make the test wait for the handler to be hit. Waiting will involve setting a reasonable timeout, which can be tricky, you don't want it too high, or failure will make the test run much longer, and not too low so that you don't get false positives.

Handler handler = mock(Handler.class);
CountDownLatch finished = new CountDownLatch(1);

doAnswer(invocation -> {
    finished.countDown();
    return null;
}).when(handler).callHandler();

TestClass testClass = new TestClass(executor, handler);

testClass.doSomething("thisThing");

boolean ended = finished.await(10, TimeUnit.SECONDS);

assertThat(ended).isTrue();

verify(handler).callHandler();

Bypassing concurrency

If you're only trying to determine whether the handler is invoked you can use an Executor that executes on the same thread. This will make for a more stable test.

Handler handler = mock(Handler.class);
Executor executor = new Executor() {
    @Override
    public void execute(Runnable command) {
        command.run();
    }
};

TestClass testClass = new TestClass(executor, handler);

testClass.doSomething("thisThing");

verify(handler).callHandler();
Share:
11,818
Ankit
Author by

Ankit

Updated on June 04, 2022

Comments

  • Ankit
    Ankit almost 2 years

    I have a code like this for which I would like to write unit test.

    public class TestClass {
    
    private final Executor executor;
    private final Handler handler;
    
    TestClass(Executor executor, Handler handler) {
        this.executor = executor;
        this.handler = handler;
    }
    
    void doSomething(String param1) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                //do something
                handler.callHandler();
            }
        });
    }
    }
    

    How can I use Mockito / Powermockito to verify if the callHandler() method is invoked.