Code example for EventBus

Methods: postregisterunregister

0
 
    @Test
    public void shouldNotReceiveEventAfterUnsubscribe() throws Exception {
 
        // given 
        EventBus eventBus = new EventBus("test");
        EventListener listener = new EventListener(); 
        EventReader reader = new EventReader();
 
        eventBus.register(listener);
        eventBus.register(reader);
 
        // when 
        eventBus.post(new OurTestEvent(200));
 
 
        // then 
        assertThat(reader.getLastMessage()).isEqualTo(200);
        assertThat(listener.getLastMessage()).isEqualTo(200); 
         
         
        //when  
        eventBus.unregister(reader);
        eventBus.post(new OurTestEvent(300));
 
 
        // then 
        assertThat(listener.getLastMessage()).isEqualTo(300); 
        // this one was unregistered