Created
December 11, 2012 10:06
-
-
Save jmaicher/4257510 to your computer and use it in GitHub Desktop.
Unit testing anonymous inner classes as event listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Unit { | |
public Unit(EventDispatcher dispatcher) { | |
dispatcher.addHandler(MyEvent.class, new IEventHandler() { | |
public void onEvent(MyEvent event) { | |
// some behavior | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UnitTest { | |
@Test | |
public void whenDispatcherTriggersMyEventThenIExpectBehaviour() { | |
EventDispatcher mockedDispatcher = mock(EventDispatcher.class); | |
new Unit(mockedDispatcher); | |
ArgumentCaptor<IEventHandler> myEventHandlerCaptor = ArgumentCaptor.forClass(IEventHandler.class); | |
verify(mockedDispatcher).addHandler(eq(MyEvent.class), myEventHandlerCaptor.capture()); | |
IEventHandler myEventHandler = myEventHandlerCaptor.getValue(); | |
MyEvent mockedEvent = mock(MyEvent.class); | |
myEventHandler.onEvent(mockedEvent); | |
// verify behavior | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Its all about naming things ;-) #ServerObserver