
在我的LoginNetworkOperation类中,我捕获了不同类型的网络连接错误,并为每个发布不同的总线事件,并且我的LoginPresenter类被注册为侦听器,并且在触发这些事件时调用某些方法.
我的问题是我是如何(并且我应该)单元测试LoginNetworkOperation是否抛出此事件而LoginPresenter是否使用Mockito处理它?
我看了这个问题:Guava EventBus unit tests但它没有提供足够的信息,特别是在实现部分.
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 }} 此外,这应该是受测试的主题,哪些应该是模拟对象?
我在AndroID Studio中使用JUnit Mockito Robolectric,我的测试工件是单元测试
解决方法My question is how (and should I) do I unit test whether the
LoginNetworkOperation throws this event and LoginPresenter handles it
with Mockito?
这已经是一个集成测试:您将各个软件模块组合在一起并将其行为验证为一个组.
在这种情况下,“受测试的主题”是LoginNetworkOperation,EventBus和LoginPresenter的组合,模拟的对象是LoginNetworkOperation的输入,以及处理LoginPresenter输出的任何内容.
这三个软件模块也可以进行单元测试:
> LoginNetworkOperation.execute:模拟EventBus并验证是否使用正确的ExceptionEvent为不同的输入调用它.> LoginPresenter.onExceptionEvent:验证不同ExceptionEvents的正确行为.> EventBus.post:注册一个模拟的ExceptionEvent-Handler,发布一个ExceptionEvent并检查是否调用了Handler.
总结以上是内存溢出为你收集整理的android – 如何/我应该用Mockito对EventBus事件进行单元测试?全部内容,希望文章能够帮你解决android – 如何/我应该用Mockito对EventBus事件进行单元测试?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)