1

我正在使用 Mockito 测试我的 GWTP 项目,但出现了一些错误:

    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
    }
}

谁能帮我解决这个问题?非常感谢!

4

2 回答 2

2

感谢 dinde在 GWTP group 的帖子,我已经解决了这个问题。

似乎测试在抱怨缺少Providerfor HttpServletRequest,所以在setUp测试中,我添加了一个提供者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; 
        } 
}
于 2011-12-12T17:03:36.503 回答
0

在测试类中使用 @Bind 和 @Mock 注释的字段

@Bind
@Mock
private HttpServletRequest request;

使用 mycila-testing-guice2 插件为我工作。

于 2012-09-09T14:17:47.593 回答