Add proper unit tests for event-aggregator pattern

This commit is contained in:
Jeroen Meulemeester 2015-12-12 20:13:25 +01:00
parent 1b74e0ff67
commit 885d5bb7dd
9 changed files with 355 additions and 0 deletions

View File

@ -13,5 +13,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,133 @@
package com.iluwatar.event.aggregator;
import org.junit.Test;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Date: 12/12/15 - 10:58 PM
*
* @author Jeroen Meulemeester
*/
public abstract class EventEmitterTest<E extends EventEmitter> {
/**
* Factory used to create a new instance of the test object with a default observer
*/
private final Function<EventObserver, E> factoryWithDefaultObserver;
/**
* Factory used to create a new instance of the test object without passing a default observer
*/
private final Supplier<E> factoryWithoutDefaultObserver;
/**
* The day of the week an event is expected
*/
private final Weekday specialDay;
/**
* The expected event, emitted on the special day
*/
private final Event event;
/**
* Create a new event emitter test, using the given test object factories, special day and event
*/
EventEmitterTest(final Weekday specialDay, final Event event,
final Function<EventObserver, E> factoryWithDefaultObserver,
final Supplier<E> factoryWithoutDefaultObserver) {
this.specialDay = specialDay;
this.event = event;
this.factoryWithDefaultObserver = Objects.requireNonNull(factoryWithDefaultObserver);
this.factoryWithoutDefaultObserver = Objects.requireNonNull(factoryWithoutDefaultObserver);
}
/**
* Go over every day of the month, and check if the event is emitted on the given day. This test
* is executed twice, once without a default emitter and once with
*/
@Test
public void testAllDays() {
testAllDaysWithoutDefaultObserver(specialDay, event);
testAllDaysWithDefaultObserver(specialDay, event);
}
/**
* Go over every day of the month, and check if the event is emitted on the given day. Use an
* event emitter without a default observer
*
* @param specialDay The special day on which an event is emitted
* @param event The expected event emitted by the test object
*/
private void testAllDaysWithoutDefaultObserver(final Weekday specialDay, final Event event) {
final EventObserver observer1 = mock(EventObserver.class);
final EventObserver observer2 = mock(EventObserver.class);
final E emitter = this.factoryWithoutDefaultObserver.get();
emitter.registerObserver(observer1);
emitter.registerObserver(observer2);
testAllDays(specialDay, event, emitter, observer1, observer2);
}
/**
* Go over every day of the month, and check if the event is emitted on the given day.
*
* @param specialDay The special day on which an event is emitted
* @param event The expected event emitted by the test object
*/
private void testAllDaysWithDefaultObserver(final Weekday specialDay, final Event event) {
final EventObserver defaultObserver = mock(EventObserver.class);
final EventObserver observer1 = mock(EventObserver.class);
final EventObserver observer2 = mock(EventObserver.class);
final E emitter = this.factoryWithDefaultObserver.apply(defaultObserver);
emitter.registerObserver(observer1);
emitter.registerObserver(observer2);
testAllDays(specialDay, event, emitter, defaultObserver, observer1, observer2);
}
/**
* Pass each week of the day, day by day to the event emitter and verify of the given observers
* received the correct event on the special day.
*
* @param specialDay The special day on which an event is emitted
* @param event The expected event emitted by the test object
* @param emitter The event emitter
* @param observers The registered observer mocks
*/
private void testAllDays(final Weekday specialDay, final Event event, final E emitter,
final EventObserver... observers) {
for (final Weekday weekday : Weekday.values()) {
// Pass each week of the day, day by day to the event emitter
emitter.timePasses(weekday);
if (weekday == specialDay) {
// On a special day, every observer should have received the event
for (final EventObserver observer : observers) {
verify(observer, times(1)).onEvent(eq(event));
}
} else {
// On any other normal day, the observers should have received nothing at all
verifyZeroInteractions(observers);
}
}
// The observers should not have received any additional events after the week
verifyNoMoreInteractions(observers);
}
}

View File

@ -0,0 +1,27 @@
package com.iluwatar.event.aggregator;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
/**
* Date: 12/12/15 - 2:52 PM
*
* @author Jeroen Meulemeester
*/
public class EventTest {
/**
* Verify if every event has a non-null, non-empty description
*/
@Test
public void testToString() {
for (final Event event : Event.values()) {
final String toString = event.toString();
assertNotNull(toString);
assertFalse(toString.trim().isEmpty());
}
}
}

View File

@ -0,0 +1,67 @@
package com.iluwatar.event.aggregator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.PrintStream;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Date: 12/12/15 - 3:04 PM
*
* @author Jeroen Meulemeester
*/
public class KingJoffreyTest {
/**
* The mocked standard out {@link PrintStream}, required since {@link KingJoffrey} does nothing
* except for writing to std-out using {@link System#out}
*/
private final PrintStream stdOutMock = mock(PrintStream.class);
/**
* Keep the original std-out so it can be restored after the test
*/
private final PrintStream stdOutOrig = System.out;
/**
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
*/
@Before
public void setUp() {
System.setOut(this.stdOutMock);
}
/**
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
*/
@After
public void tearDown() {
System.setOut(this.stdOutOrig);
}
/**
* Test if {@link KingJoffrey} tells us what event he received
*/
@Test
public void testOnEvent() {
final KingJoffrey kingJoffrey = new KingJoffrey();
for (final Event event : Event.values()) {
verifyZeroInteractions(this.stdOutMock);
kingJoffrey.onEvent(event);
final String expectedMessage = "Received event from the King's Hand: " + event.toString();
verify(this.stdOutMock, times(1)).println(expectedMessage);
verifyNoMoreInteractions(this.stdOutMock);
}
}
}

View File

@ -0,0 +1,48 @@
package com.iluwatar.event.aggregator;
import org.junit.Test;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Date: 12/12/15 - 10:57 AM
*
* @author Jeroen Meulemeester
*/
public class KingsHandTest extends EventEmitterTest<KingsHand> {
/**
* Create a new test instance, using the correct object factory
*/
public KingsHandTest() {
super(null, null, KingsHand::new, KingsHand::new);
}
/**
* The {@link KingsHand} is both an {@EventEmitter} as an {@link EventObserver} so verify if every
* event received is passed up to it's superior, in most cases {@link KingJoffrey} but now just a
* mocked observer.
*/
@Test
public void testPassThrough() throws Exception {
final EventObserver observer = mock(EventObserver.class);
final KingsHand kingsHand = new KingsHand(observer);
// The kings hand should not pass any events before he received one
verifyZeroInteractions(observer);
// Verify if each event is passed on to the observer, nothing less, nothing more.
for (final Event event : Event.values()) {
kingsHand.onEvent(event);
verify(observer, times(1)).onEvent(eq(event));
verifyNoMoreInteractions(observer);
}
}
}

View File

@ -0,0 +1,17 @@
package com.iluwatar.event.aggregator;
/**
* Date: 12/12/15 - 10:57 AM
*
* @author Jeroen Meulemeester
*/
public class LordBaelishTest extends EventEmitterTest<LordBaelish> {
/**
* Create a new test instance, using the correct object factory
*/
public LordBaelishTest() {
super(Weekday.FRIDAY, Event.STARK_SIGHTED, LordBaelish::new, LordBaelish::new);
}
}

View File

@ -0,0 +1,17 @@
package com.iluwatar.event.aggregator;
/**
* Date: 12/12/15 - 10:57 AM
*
* @author Jeroen Meulemeester
*/
public class LordVarysTest extends EventEmitterTest<LordVarys> {
/**
* Create a new test instance, using the correct object factory
*/
public LordVarysTest() {
super(Weekday.SATURDAY, Event.TRAITOR_DETECTED, LordVarys::new, LordVarys::new);
}
}

View File

@ -0,0 +1,17 @@
package com.iluwatar.event.aggregator;
/**
* Date: 12/12/15 - 10:57 AM
*
* @author Jeroen Meulemeester
*/
public class ScoutTest extends EventEmitterTest<Scout> {
/**
* Create a new test instance, using the correct object factory
*/
public ScoutTest() {
super(Weekday.TUESDAY, Event.WARSHIPS_APPROACHING, Scout::new, Scout::new);
}
}

View File

@ -0,0 +1,24 @@
package com.iluwatar.event.aggregator;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Date: 12/12/15 - 2:12 PM
*
* @author Jeroen Meulemeester
*/
public class WeekdayTest {
@Test
public void testToString() throws Exception {
for (final Weekday weekday : Weekday.values()) {
final String toString = weekday.toString();
assertNotNull(toString);
assertEquals(weekday.name(), toString.toUpperCase());
}
}
}