#113 Event Driven Architecture

Adds unit test to assert and verify pattern event get type behaviour.
Also added unit test comments.
This commit is contained in:
cfarrugia 2015-11-28 17:58:32 +01:00
parent 3ad36020aa
commit d9a1d1cef9

View File

@ -1,4 +1,5 @@
import com.iluwatar.eda.EventDispatcher; import com.iluwatar.eda.EventDispatcher;
import com.iluwatar.eda.event.Event;
import com.iluwatar.eda.event.UserCreatedEvent; import com.iluwatar.eda.event.UserCreatedEvent;
import com.iluwatar.eda.event.UserUpdatedEvent; import com.iluwatar.eda.event.UserUpdatedEvent;
import com.iluwatar.eda.handler.UserCreatedEventHandler; import com.iluwatar.eda.handler.UserCreatedEventHandler;
@ -6,17 +7,22 @@ import com.iluwatar.eda.handler.UserUpdatedEventHandler;
import com.iluwatar.eda.model.User; import com.iluwatar.eda.model.User;
import org.junit.Test; import org.junit.Test;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/**
* Event Driven Pattern unit tests to assert and verify correct pattern behaviour
*/
public class EventDrivenTest { public class EventDrivenTest {
/**
* This unit test should register events and event handlers correctly with the event dispatcher
* and events should be dispatched accordingly.
*/
@Test @Test
public void testEventDriverPattern(){ public void testEventDriverPattern() {
EventDispatcher dispatcher = spy(new EventDispatcher()); EventDispatcher dispatcher = spy(new EventDispatcher());
UserCreatedEventHandler userCreatedEventHandler = new UserCreatedEventHandler(); UserCreatedEventHandler userCreatedEventHandler = new UserCreatedEventHandler();
@ -27,10 +33,10 @@ public class EventDrivenTest {
assertEquals("Two handlers must be registered", 2, dispatcher.getHandlers().size()); assertEquals("Two handlers must be registered", 2, dispatcher.getHandlers().size());
assertEquals("UserCreatedEvent must return the UserCreatedEventHandler", assertEquals("UserCreatedEvent must return the UserCreatedEventHandler",
userCreatedEventHandler, userCreatedEventHandler,
(UserCreatedEventHandler)dispatcher.getHandlers().get(UserCreatedEvent.class)); (UserCreatedEventHandler) dispatcher.getHandlers().get(UserCreatedEvent.class));
assertEquals("UserUpdatedEvent must be registered to the UserUpdatedEventHandler", assertEquals("UserUpdatedEvent must be registered to the UserUpdatedEventHandler",
userUpdatedEventHandler, userUpdatedEventHandler,
(UserUpdatedEventHandler)dispatcher.getHandlers().get(UserUpdatedEvent.class)); (UserUpdatedEventHandler) dispatcher.getHandlers().get(UserUpdatedEvent.class));
User user = new User("iluwatar"); User user = new User("iluwatar");
@ -42,6 +48,16 @@ public class EventDrivenTest {
//verify that the events have been dispatched //verify that the events have been dispatched
verify(dispatcher).dispatch(userCreatedEvent); verify(dispatcher).dispatch(userCreatedEvent);
verify(dispatcher).dispatch(userUpdatedEvent); verify(dispatcher).dispatch(userUpdatedEvent);
}
/**
* This unit test should correctly return the {@link Event} class type when calling the
* {@link Event#getType() getType} method.
*/
@Test
public void testGetEventType() {
User user = new User("iluwatar");
UserCreatedEvent userCreatedEvent = new UserCreatedEvent(user);
assertEquals(UserCreatedEvent.class, userCreatedEvent.getType());
} }
} }