使用 Mockito 测试 GWTP

发布于 2024-12-20 16:39:33 字数 1496 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-12-27 16:39:33

感谢 dinde 在 GWTP 群组中发帖 ,我已经解决了这个问题。

似乎测试抱怨 HttpServletRequest 缺少 Provider,因此在测试的 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; 
        } 
}

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; 
        } 
}
ㄖ落Θ余辉 2024-12-27 16:39:33

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

@Bind
@Mock
private HttpServletRequest request;

适用于使用 mycila-testing-guice2 插件的我。

The field annotated both with @Bind and @Mock inside a test class

@Bind
@Mock
private HttpServletRequest request;

works for me using mycila-testing-guice2 plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文