Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public class FakeEventBinderProvider implements FakeProvider<EventBinder<?>> {
public EventBinder<?> getFake(Class<?> type) {
return (EventBinder<?>) Proxy.newProxyInstance(FakeEventBinderProvider.class.getClassLoader(), new Class<?>[] { type }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, final Object[] args) throws Throwable {
String methodName = method.getName();
assert methodName.equals("bindEventHandlers");
final List<HandlerRegistration> registrations = new LinkedList<HandlerRegistration>();
EventBus eventBus = (EventBus) args[1];
List<Method> presenterMethods = getAllMethods(args[0].getClass());
for (final Method presenterMethod : presenterMethods) {
if (presenterMethod.isAnnotationPresent(EventHandler.class)) {
@SuppressWarnings("unchecked") // Should always be ok, since the Generator for EventBinder should do all the safe-checking
Class<? extends GenericEvent> eventType = (Class<? extends GenericEvent>) (presenterMethod.getParameterTypes())[0];
registrations.add(eventBus.addHandler(GenericEventType.getTypeOf(eventType), new GenericEventHandler() {
public void handleEvent(GenericEvent event) {
try {
presenterMethod.invoke(args[0], event);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
return new HandlerRegistration() {
public void removeHandler() {
for (HandlerRegistration registration : registrations) {
private List<Method> getAllMethods(Class<?> type) {
List<Method> methods = new LinkedList<Method>();
if (type.getSuperclass() != null) {
return methods;
public void setUpEventBindery() {
GwtMockito.useProviderForType(EventBinder.class, new FakeEventBinderProvider());

This comment has been minimized.

Copy link

@kfahy kfahy commented Feb 4, 2015

Thanks a lot for writing this - it was super useful in helping get integration testing working in a GWT app via Guice.

Since gwteventbinder has been updated to support handling multiple event types in a single handler, I've hacked up support for calling methods based on the event types in their EventHandler annotation. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.