Guava EventBus单元测试 [英] Guava EventBus unit tests

查看:166
本文介绍了Guava EventBus单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Guava EventBus,其中包含一个简单的事件和一个简单的侦听器.我的问题是在事件发布后检查是否调用了listener方法的测试模式是什么.

我建议测试EventBus正常工作不是您应该编写的UNIT测试.使用库(至少使用您信任的库)的优点之一是单元测试是由库提供程序编写的.因此,请不要浪费您的时间来验证Google人员是否正确编写了EventBus.要编写系统的单元测试,应模拟EventBus,因此将不会调用您的侦听器.这是使用消息总线的优势之一,它可以隔离单独的应用程序问题,从而使单元测试更加容易.

当您准备好进行此操作时,它将是一个集成测试,用于测试整个系统是否可以协同工作.在某些情况下,这也可能是用JUnit编写的,但不要认为这是一个单元测试.如何执行此操作取决于您的系统.您可以将Spring上下文加载到JUnit测试中,也可以部署应用程序并对它运行测试.

I have a simple Guava EventBus with one simple event and one simple listener. My question is what is the test pattern to check if the listener method is invoked once the event is posted.

解决方案

I would suggest that testing that the EventBus works properly is not a UNIT test you should be writing. One of the advantages of using a library (at least of using one you trust) is that the unit tests have been written by the library provider. So please don't waste your time verifying that the Google folks wrote EventBus properly. To write unit tests of your system the EventBus should be mocked and therefore your listener would not be invoked. This is one of the advantages of using a message bus, it allows for isolation of separate application concerns which allows for easier unit testing.

When you are ready to do so, it would be an integration test that tests that the entire system works together. In some cases this might also be written in JUnit but don't think that it is a unit test. How to do this depends on your system. You might load up a Spring context into a JUnit test or you might deploy the application and run tests against it.

这篇关于Guava EventBus单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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