gpt4 book ai didi

java - 使用 Mockito 测试 GWTP

转载 作者:行者123 更新时间:2023-11-30 04:56:13 24 4
gpt4 key购买 nike

我正在使用 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
}
}

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

最佳答案

感谢dinde的post in GWTP group ,我已经解决了这个问题。

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

关于java - 使用 Mockito 测试 GWTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8457609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com