2011-12-10 95 views
1

我使用的Mockito來測試我的GWTP項目,我得到了一些錯誤:測試GWTP用的Mockito

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) 

下面是單元測試代碼:

@Mock 
private TestActionHandler mockTestActionHandler; 

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

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

這裏是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 
    } 
} 

任何人都可以幫我解決這個問題嗎?太多了!

+0

你的ServerModule和MockHandlerModule類是怎麼樣的? –

回答

2

感謝dinde的post in GWTP group,我已經解決了這個問題。

看來,測試抱怨缺少ProviderHttpServletRequest,所以在測試的setUp,我添加提供程序FO的HttpServletRequest,問題就解決了。這裏的工作代碼:

@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; 
     } 
} 
0

使用mycila測試-guice2插件都與@Bind和@Mock註解測試類中

@Bind 
@Mock 
private HttpServletRequest request; 

對我的作品的字段。