How to/Should I unit test EventBus events with Mockito?

Viewed 3815

I use Otto's EventBus in my Android app.

In my LoginNetworkOperation class, I catch different kinds of network connection errors and I post different bus events for each one, and my LoginPresenter class is registered as a listener and certain methods are invoked, when these events are fired.

My question is how (and should I) do I unit test whether the LoginNetworkOperation throws this event and LoginPresenter handles it with Mockito?

I looked at this question: Guava EventBus unit tests but it doesn't provide enough information, especially on the implementation part.

public class LoginNetworkOperation {

    public void execute(String username, String password) {
        try {
            // retrofit attempt
        } catch (Exception e) {
            bus.post(new ExceptionEvent(e));
        }
    }
}

public class LoginPresenter {

    void onExceptionEvent(ExceptionEvent exceptionEvent) {
        // Do Something with the exception event
    }
}

Also, which should be the Subject Under Test here and which should be the mocked object?

I am using JUnit + Mockito + Robolectric in Android Studio and my test artifact is Unit Tests

1 Answers
Related