Skip to content

Commit b2fcd4a

Browse files
author
Stephen Braimah
committed
BAEL-639: Guide to Guava's EventBus Tests
1 parent 03902d2 commit b2fcd4a

4 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.guava;
2+
3+
4+
public class CustomEvent {
5+
private String action;
6+
7+
public CustomEvent(String action) {
8+
this.action = action;
9+
}
10+
11+
public String getAction() {
12+
return action;
13+
}
14+
15+
public void setAction(String action) {
16+
this.action = action;
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.guava;
2+
3+
import com.google.common.eventbus.EventBus;
4+
5+
class EventBusWrapper {
6+
7+
private static EventBus eventBus = new EventBus();
8+
9+
static void register(Object object){
10+
eventBus.register(object);
11+
}
12+
13+
static void unregister(Object object){
14+
eventBus.unregister(object);
15+
}
16+
17+
static void post(Object object){
18+
eventBus.post(object);
19+
}
20+
21+
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.guava;
2+
import com.google.common.eventbus.Subscribe;
3+
4+
public class EventListener {
5+
6+
private static int eventsHandled;
7+
8+
/**
9+
* Handles events of type String *
10+
*/
11+
@Subscribe
12+
public void stringEvent(String event){
13+
System.out.println("do event ["+event+"]");
14+
eventsHandled++;
15+
}
16+
17+
/**
18+
* Handles events of type CustomEvent
19+
*/
20+
@Subscribe
21+
public void someEvent(CustomEvent customEvent){
22+
System.out.println("do event ["+ customEvent.getAction()+"]");
23+
eventsHandled++;
24+
}
25+
26+
public int getEventsHandled() {
27+
return eventsHandled;
28+
}
29+
30+
public void resetEventsHandled(){
31+
eventsHandled = 0;
32+
}
33+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.guava;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import static org.junit.Assert.*;
7+
8+
public class GuavaEventBusTest {
9+
10+
private EventListener listener;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
listener = new EventListener();
15+
EventBusWrapper.register(listener);
16+
}
17+
18+
@After
19+
public void tearDown() throws Exception {
20+
EventBusWrapper.unregister(listener);
21+
}
22+
23+
@Test
24+
public void givenStringEvent_whenEventHandled_thenSuccess() throws Exception {
25+
listener.resetEventsHandled();
26+
27+
EventBusWrapper.post("String Event");
28+
assertEquals(1,listener.getEventsHandled());
29+
30+
}
31+
32+
@Test
33+
public void givenCustomEvent_whenEventHandled_thenSuccess() throws Exception {
34+
listener.resetEventsHandled();
35+
36+
CustomEvent customEvent = new CustomEvent("Custom Event");
37+
EventBusWrapper.post(customEvent);
38+
39+
assertEquals(1,listener.getEventsHandled());
40+
}
41+
}

0 commit comments

Comments
 (0)