mardi 3 novembre 2015

Android MVP unit testing - should I mock event bus?

Scenario: user performs an action on the screen, which make application load some data asynchronously and later update the view.

In my architecture it's done in this way:

  1. User's action calls some presenter method
  2. Presenter calls manager, which starts an async task
  3. Async task in background thread calls a service, and posts the result to event bus
  4. When presenter is notified about new data (by event bus), it reloads the view

I am thinking about two approaches to unit testing in that situation (let's assume async task is executed sequentially in single test thread):

  1. It doesn't make sense test each class in separation, because methods usually have no logic, only delegation. We can define "the unit" at the level of presenter and its dependencies, up to service level (so, we should mock only the service). The result returned from service should be eventually delivered to presenter, so we can test full path.

  2. Event bus implementation we are using depends on Android runtime (Green Robot), so to use it in test we would create some smart fake, which is quite complicated. So, it would be better for unit test should mock event bus as an "external" dependency. So for our use case, we should separately test path presenter -> event bus and event bus -> presenter.

Which approach would be better for my scenario?

The first one seems more intuitive: tests are checking if user action results in correct view change. If we could perform an action synchronously, manager, async task and event bus would be unnecesary - they're just boilerplate, irrelevant for business logic. Am I missing something which I would regret?

Aucun commentaire:

Enregistrer un commentaire