2

How do i test an event bus in android . I am using Otto. I have tried:

@Mock 
Bus bus;

delcared in my test. But now what i want to do is post an event and then check if the action was performed. The event is created like this:

new CoolEvent(true,true); but i dont know the command to check if the subscriber code executed. Can i use Captor ? How is this done ?

j2emanue
  • 60,549
  • 65
  • 286
  • 456

1 Answers1

4

I already found out how to test it. There are two things you can do. One is use Mockito and do this:

 ArgumentCaptor<OTTOEventClass> captor = ArgumentCaptor
                   .forClass(OTTOEventClass.class);

then since you hopefully have a bus in your test you can verify that a post to the bus was made with the OTTOEventClass object. like this:

verify(bus).post(captor.capture());

you could also verify certain flags in your OTTOEventClass are set like this:

assertThat(captor.getValue().isFlagSet(), equalTo(true));

The other thing you can do is just verify that a bus post was called with some type like this:

verify(bus).post(isA(OTTOEventClass.class)); 

the above just verifies bus.post(new OTTOEventClass()) was called somewhere during your test and that the event that was posted is of type OTTOEventClass.

I dont test the otto subscriber because my tests are about testing my functionality not Otto. Let Otto be tested by square, i dont test framework i test my code & calls and my business logic.

j2emanue
  • 60,549
  • 65
  • 286
  • 456