com.gwtplatform.tester
Class MockHandlerModule

java.lang.Object
  extended by com.google.inject.AbstractModule
      extended by com.gwtplatform.tester.MockHandlerModule
All Implemented Interfaces:
Module

public abstract class MockHandlerModule
extends AbstractModule

Module for use in test cases when creating a guice injector that needs to provide mock handlers. Your injector must also have an class that subclasses guice.HandlerModule to bind Actions to ActionHandlers and ActionValidators.

You should subclass this module and use the configureMockHandlers() method to:

Unit Testing Example

  // create mock handlers
  CreateFooActionHandler mockCreateFooActionHandler =
       mock(CreateFooActionHandler.class);

  GeocodeAddressClientActionHandler geocodeAddressClientActionHandler =
       mock(GeocodeAddressClientActionHandler.class);

  // create injector
  Injector injector =
      Guice.createInjector(new MyHandlerModule(),
          new MockHandlerModule() {
            @Override
            protected void configureMockHandlers() {
              bindMockActionHandler(
                  CreateFooActionHandler.class,
                  mockCreateFooActionHandler);
              }
              bindMockClientActionHandler(
                  GeocodeAddressAction.class,
                  geocodeAddressClientActionHandler);
            });

  // get dispatcher
  DispatchAsync dispatcher = injector.getInstance(DispatchAsync.class);

  // create mock result
  final CreateFooResult result =
      new CreateFooResult(new Key(Foo.class, 1));

  // configure mockito to return mock result on specific action
  when(
    businessCreateActionHandler.execute(
        eq(new CreateFooAction("Bar")),
          any(ExecutionContext.class))).thenReturn(result);

  // configuring mockito to return result for clent action handler
  // is a bit more complex
  final GeocodeAddressResult geocodeAddressResult = new GeocodeAddressResult(...);
  doAnswer(new Answer<Object>() {
    @SuppressWarnings("unchecked")
    public Object answer(InvocationOnMock invocation) {
      AsyncCallback<GeocodeAddressResult> callback
          = (AsyncCallback<GeocodeAddressResult>) invocation.getArguments()[1];
      callback.onSuccess(new GeocodeAddressResult(geocodeAddressResult));
      return null;
    }
  }).when(geocodeAddressClientActionHandler)
       .execute(
           eq(new GeocodeAddressAction("2 Google Way, New Zealand",
               "nz")), any(AsyncCallback.class),
           any(ClientDispatchRequest.class), any(ExecuteCommand.class));

 

Author:
Brendan Doherty

Constructor Summary
MockHandlerModule()
           
 
Method Summary
protected
<A extends Action<R>,R extends Result,H extends ActionHandler<A,R>>
void
bindMockActionHandler(Class<H> handlerClass, H mockHandler)
          Registers a mock server-side action handlers.
protected
<A extends Action<R>,R extends Result,H extends AbstractClientActionHandler<A,R>>
void
bindMockClientActionHandler(Class<A> actionClass, H mockHandler)
          Registers a mock client-side action handlers.
protected
<A extends Action<R>,R extends Result,H extends ActionHandler<A,R>>
void
bindMockHandler(Class<H> handler, H mockHandler)
          Deprecated. 
protected  void configure()
           
protected abstract  void configureMockHandlers()
           
 
Methods inherited from class com.google.inject.AbstractModule
addError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBinding
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MockHandlerModule

public MockHandlerModule()
Method Detail

configure

protected void configure()
Specified by:
configure in class AbstractModule

configureMockHandlers

protected abstract void configureMockHandlers()

bindMockHandler

@Deprecated
protected <A extends Action<R>,R extends Result,H extends ActionHandler<A,R>> void bindMockHandler(Class<H> handler,
                                                                                                              H mockHandler)
Deprecated. 

Use bindMockActionHandler instead.


bindMockActionHandler

protected <A extends Action<R>,R extends Result,H extends ActionHandler<A,R>> void bindMockActionHandler(Class<H> handlerClass,
                                                                                                         H mockHandler)
Registers a mock server-side action handlers.

This mock server-side action handler will be executed when the class under test calls DispatchAsync#execute() or DispatchAsync#undo().

Type Parameters:
A - Type of Action that will be executed by mock handler
R - Type of Result that will be returned by mock handler
H - Type of the mock handler that extends ActionHandler
Parameters:
handlerClass - The type of the mock server-side handler
mockHandler - Instance of the ActionHandler to execute actions of type <A>

bindMockClientActionHandler

protected <A extends Action<R>,R extends Result,H extends AbstractClientActionHandler<A,R>> void bindMockClientActionHandler(Class<A> actionClass,
                                                                                                                             H mockHandler)
Registers a mock client-side action handlers.

This mock client-side action handler will be executed when the class under test calls DispatchAsync#execute() or DispatchAsync#undo().

If both mock client and mock server action handlers have been registered, the server side action handler will only be called if the mock client side action handler calls ExecuteCommand#execute() or UndoCommand#undo()

Type Parameters:
A - Type of Action
R - Type of Result
H - Type of AbstractClientActionHandler
Parameters:
actionClass - Implementation of ActionHandler to link and bind
mockHandler - Instance of the ActionHandler to execute actions of type <A>


Copyright © 2010-2011 ArcBees. All Rights Reserved.