GWT RPC GWTTestCase + GUICE 2.0

时间:2011-07-13 16:09:35

标签: gwt guice gwttestcase

我正在尝试使用前端的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);
  }

`/ **

然而,当我运行测试时,它会失败并在控制台上打印

[WARN] 404 - POST /com.resume.Contacthandler.JUnit/GWT.rpc(192.168.0.11)1425 bytes    请求标头       主持人:192.168.0.11:4016       User-Agent:Mozilla / 5.0(Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19)Gecko / 2010031422 Firefox / 3.0.19       接受语言:en-us       接受: /       连接:保持活力       参考文献: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/       Content-Type:text / x-gwt-rpc;字符集= utf-8的       内容长度:285    响应标头       内容类型:text / html;字符集= 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?

1 个答案:

答案 0 :(得分:1)

除了博客中的方法之外,还有更简单的方法可以让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
}