0

我正在尝试使用 GWT 在前端创建一个应用程序,而在后端使用 GUICE 在 Google App Engine 上提供服务。

我使用示例设置创建了一个非常简单的应用程序

http://stuffthathappens.com/blog/2009/09/14/guice-with-gwt/#comment-49355

该应用程序运行良好,但是我想为 GWT RPC 调用添加一些单元测试。

我正在尝试使用 GWTTestCase,如下所示:`public void testContactMessageService() {

    ContactMessage message = new ContactMessage();
    message.setName("Jeff");
    message.setMessage("Just wanted to say I'm a fan.");
    message.setEmail("man.nick.utd@gmail.com");

    ContactMessageServiceAsync contactMessageService = GWT.create(ContactMessageService.class);

    contactMessageService.sendMessage(message, 
                new AsyncCallback<String>() {
                    public void onFailure(Throwable caught) {
                        // Show the RPC error message to the user
                        System.out.println(caught);
                        fail("big time failure");
                        finishTest();
                    }

                    public void onSuccess(String result) {
                        System.out.println("success, biatch");
                        assertTrue(true);
                        finishTest();
                    }
                });
      delayTestFinish(1000);
  }

`/**

但是,当我运行测试它失败并在控制台上打印

[警告] 404 - POST /com.resume.Contacthandler.JUnit/GWT.rpc (192.168.0.11) 1425 字节请求标头主机:192.168.0.11:4016 用户代理:Mozilla/5.0(Windows;U;Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19 Accept-Language: en-us Accept: / 连接:保持活动引用:192.168.0.11:4016/com.resume.Contacthandler.JUnit/junit.html?gwt.codesvr=192.168.0.11:4012 X-GWT-Permutation:HostedMode X-GWT-Module-Base:192.168 .0.11:4016/com.resume.Contacthandler.JUnit/内容类型:文本/x-gwt-rpc;charset=utf-8 Content-Length: 285 响应头 Content-Type: text/html; charset=iso-8859-1 内容长度:1425 com.google.gwt.user.client.rpc.StatusCodeException:404 HTTP 错误:404 NOT_FOUND RequestURI=/com.resume.Contacthandler.JUnit/GWT.rpc

从这个输出中,我假设 Guice 的服务器端没有设置。

运行 GWTTestCases 时如何设置服务器端 Guice servlet?

4

1 回答 1

1

除了 stuffthathappens 博客中的方法之外,还有更简单的方法可以让 Guice 和 GWT 工作。例如,以下代码是启动和运行 servlet 所需的大部分代码。这不涉及任何 GWT 代码,因此很容易使用纯 JRE 测试进行测试 - 您只需要设置一个测试注入器并获取 Service Impl 的实例。

serve("/myapp/importservice").with(SourceImportServiceImpl.class);


@Singleton
public class SourceImportServiceImpl extends RemoteServiceServlet {

  private Provider<SimpleDao> daoProvider;

  @Inject
  public SourceImportServiceImpl(Provider<SimpleDao> daoProvider) {
    this.daoProvider = daoProvider;
  }

 ... RPC method implementations
}
于 2011-08-01T16:13:33.517 回答