如何/应该使用 Mockito 对 EventBus 事件进行单元测试? [英] How to/Should I unit test EventBus events with Mockito?

查看:81
本文介绍了如何/应该使用 Mockito 对 EventBus 事件进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Android 应用中使用 Otto 的 EventBus.

I use Otto's EventBus in my Android app.

在我的 LoginNetworkOperation 类中,我捕获了不同类型的网络连接错误,并为每个错误发布了不同的总线事件,并且我的 LoginPresenter 类被注册为侦听器和当这些事件被触发时,会调用某些方法.

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.

我的问题是如何(以及我应该)如何单元测试 LoginNetworkOperation 是否抛出此事件并且 LoginPresenter 是否使用 Mockito 处理它?

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

我看了这个问题:Guava EventBus 单元测试但它没有提供足够的信息,尤其是关于实施部分的信息.

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
    }
}

另外,哪个应该是Subject Under Test,哪个应该是模拟对象?

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

我在 Android Studio 中使用 JUnit + Mockito + Robolectric,我的测试工件是单元测试

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

推荐答案

我的问题是如何(以及我应该)如何进行单元测试LoginNetworkOperation 抛出这个事件,LoginPresenter 处理它用 Mockito?

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.

在这种情况下,被测对象"是 LoginNetworkOperationEventBusLoginPresenter 的组合,模拟对象是LoginNetworkOperation 以及处理 LoginPresenter 输出的任何东西.

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:模拟 EventBus 并验证它是否针对不同的输入使用正确的 ExceptionEvent 调用.
  • LoginPresenter.onExceptionEvent:验证不同 ExceptionEvents 的正确行为.
  • EventBus.post:注册一个模拟的ExceptionEvent-Handler,发布一个ExceptionEvent并检查Handler是否被调用.
  • 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.

这篇关于如何/应该使用 Mockito 对 EventBus 事件进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆