I'm a little late to the party, but here is an example of a class that works and takes into account asynchronous calls. Instead of the Mocking EventBus, we just let it do this and register it in the TestDriver class below.
What does this work is a CountDownLatch , which, using the abstract DataTransferCallback class DataTransferCallback waits for latch.countDown() be called, or after 5 seconds.
Just register your test class and @Subscribe method, pass it back to the method that created the DataTransferCallback , and make your statements there.
@RunWith(AndroidJUnit4.class) public class TestDriver { private final CountDownLatch latch = new CountDownLatch(1); private EventBus eventBus; private DataTransferCallback transferCallback; public abstract class DataTransferCallback { abstract void onSuccess(DataTransfer event); } @Before public void setUp() { EventBus.getDefault().register(this); eventBus = spy(EventBus.getDefault()); } @SuppressWarnings("unchecked") @Test public void test200Resposne() throws InterruptedException {
source share