BAEL-639: Guide to Guava's EventBus Tests - removed EventWrapper and added DeadEvent Subscriber
This commit is contained in:
parent
80c07b0ab2
commit
2ab1c19561
@ -1,21 +0,0 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
class EventBusWrapper {
|
||||
|
||||
private static EventBus eventBus = new EventBus();
|
||||
|
||||
static void register(Object object) {
|
||||
eventBus.register(object);
|
||||
}
|
||||
|
||||
static void unregister(Object object) {
|
||||
eventBus.unregister(object);
|
||||
}
|
||||
|
||||
static void post(Object object) {
|
||||
eventBus.post(object);
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.DeadEvent;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -21,6 +22,12 @@ public class EventListener {
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void handleDeadEvent(DeadEvent deadEvent) {
|
||||
LOG.info("unhandled event [" + deadEvent.getEvent() + "]");
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
public int getEventsHandled() {
|
||||
return eventsHandled;
|
||||
}
|
||||
|
@ -1,32 +1,36 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class GuavaEventBusTest {
|
||||
|
||||
private EventListener listener;
|
||||
private EventBus eventBus;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
eventBus = new EventBus();
|
||||
listener = new EventListener();
|
||||
EventBusWrapper.register(listener);
|
||||
|
||||
eventBus.register(listener);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
EventBusWrapper.unregister(listener);
|
||||
eventBus.unregister(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringEvent_whenEventHandled_thenSuccess() {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
EventBusWrapper.post("String Event");
|
||||
eventBus.post("String Event");
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -34,8 +38,17 @@ public class GuavaEventBusTest {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
CustomEvent customEvent = new CustomEvent("Custom Event");
|
||||
EventBusWrapper.post(customEvent);
|
||||
eventBus.post(customEvent);
|
||||
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnSubscribedEvent_whenEventHandledByDeadEvent_thenSuccess() {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
eventBus.post(12345);
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user