Search code examples
javagwtguicemockitogwt-platform

Testing GWTP with Mockito


I am using Mockito to test my GWTP project and I got some error:

    com.google.inject.CreationException: Guice creation errors:

1) No implementation for javax.servlet.http.HttpServletRequest was bound.
  while locating com.google.inject.Provider<javax.servlet.http.HttpServletRequest>
    for parameter 0 at com.gwtplatform.dispatch.server.guice.request.DefaultRequestProvider.<init>(DefaultRequestProvider.java:35)
  at com.gwtplatform.dispatch.server.guice.DispatchModule.configure(DispatchModule.java:135)

Below is the code for unit test:

@Mock
private TestActionHandler mockTestActionHandler;

@Before
public void setUp() {
    Injector injector = Guice.createInjector(new ServerModule(), new MockHandlerModule() {

        @Override
        protected void configureMockHandlers() {
                bindMockActionHandler(TestActionHandler.class, 
                        mockTestActionHandler);
            }
        }); 
}

Here is the code for TestActionHandler:

public class TestActionHandler implements ActionHandler<TestAction, TestResult> {

    private final Provider<HttpServletRequest> provider;

    @Inject
    public RetrieveEventsUsingCategoryIdActionHandler(
            final Provider<HttpServletRequest> provider) {
        this.provider = provider;
    }

    @Override
    public TestResult execute(TestAction action, ExecutionContext context) {
        //handle action
    }
}

Could anyone help me fix this? Thaks a lot!


Solution

  • Thanks to dinde's post in GWTP group, I have resolved this problem.

    It seems that the test is complaining about missing Provider for HttpServletRequest, so in the setUp of the test, I add a provider fo the HttpServletRequest and the problem is solved. Here's the working code:

    @Mock
    private TestActionHandler mockTestActionHandler;
    @Mock
    private HttpServletRequest servletRequest;
    
    @Before
    public void setUp() {
        Injector injector = Guice.createInjector(new ServerModule(), new MockHandlerModule() {
    
            @Override
            protected void configureMockHandlers() {
                    bindMockActionHandler(TestActionHandler.class, 
                            mockTestActionHandler);
                }
            });
    
            @SuppressWarnings("unused")
            @Provides 
            public HttpServletRequest createServletRequest() { 
                return servletRequest; 
            } 
    }