Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava EventBus unit tests

Tags:

java

junit

guava

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.

like image 913
speedingdeer Avatar asked May 07 '13 08:05

speedingdeer


2 Answers

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.

like image 131
John B Avatar answered Oct 20 '22 07:10

John B


This kind of testing is usually done by using a mocking framework like Mockito. Create a mock listener, register it with the event bus, fire the event, and verify that the listener method got invoked.

Find here a very basic example on how to create a mock and how to verify interactions to it.

like image 25
zagyi Avatar answered Oct 20 '22 05:10

zagyi