使用 Mockito 测试 GWTP
我正在使用 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
}
}
有人能帮我解决这个问题吗?非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢 dinde 在 GWTP 群组中发帖 ,我已经解决了这个问题。
似乎测试抱怨
HttpServletRequest
缺少Provider
,因此在测试的setUp
中,我为添加了一个提供程序>HttpServletRequest
问题就解决了。这是工作代码:Thanks to dinde's post in GWTP group, I have resolved this problem.
It seems that the test is complaining about missing
Provider
forHttpServletRequest
, so in thesetUp
of the test, I add a provider fo theHttpServletRequest
and the problem is solved. Here's the working code:在测试类中使用 @Bind 和 @Mock 注释的字段
适用于使用 mycila-testing-guice2 插件的我。
The field annotated both with @Bind and @Mock inside a test class
works for me using mycila-testing-guice2 plugin.