4

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

Community
  • 1
  • 1
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

1 Answers1

2

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

That's already an integration test: you're combining individual software modules and verifying their behaviour as a group.

In that case the "subject under test" is the combination of LoginNetworkOperation, EventBus and LoginPresenter and the mocked objects are the inputs to LoginNetworkOperation and whatever handles the output of LoginPresenter.


Those three software modules can also be unit tested:

  • LoginNetworkOperation.execute: mock the EventBus and verify that it's called with the correct ExceptionEvent for different inputs.
  • LoginPresenter.onExceptionEvent: verify correct behaviour for different ExceptionEvents.
  • EventBus.post: register a mocked ExceptionEvent-Handler, post an ExceptionEvent and check that the Handler is called.
Bewusstsein
  • 1,591
  • 13
  • 19