GWT RPC GWTTestCase + GUICE 2.0 [英] GWT RPC GWTTestCase + GUICE 2.0

查看:132
本文介绍了GWT RPC GWTTestCase + GUICE 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用前端的GWT和Google App Engine上后端的GUICE创建应用程序。



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

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(只想说我是粉丝。);
message.setEmail(man.nick.utd@gmail.com);

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

contactMessageService.sendMessage(message,
new AsyncCallback< String>(){
public void onFailure(Throwable caught){
//显示RPC错误信息用户
System.out.println(已捕获);
失败(大时间失败);
finishTest();
}

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

`/ **

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


$ b

[WARN] 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
接受语言:en-us
接受: /
连接:保持活动
Referer: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; charset = utf-8
Content-Length:285
响应标题
Content-Type:text / html; charset = iso-8859-1
Content-Length:1425
com.google.gwt.user.client.rpc.StatusCodeException:404
HTTP错误:404 NOT_FOUND
RequestURI = /com.resume.Contacthandler.JUnit/GWT.rpc

从这个输出中,我假设服务器端的东西与Guice没有进行安装。



如何在运行GWTTestCases时设置服务器端的Guice Servlet? 解决方案

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


Im trying to create an app with the GWT on the front end and GUICE on the backend served on the Google App Engine.

I have created a very simple app using the example setup

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

The app works fine, however I wanted to add some unit tests for the GWT RPC calls.

I am trying to use the GWTTestCase as below: `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);
  }

`/**

However when I run the test it fails and on the console it prints

[WARN] 404 - POST /com.resume.Contacthandler.JUnit/GWT.rpc (192.168.0.11) 1425 bytes Request headers Host: 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 Accept-Language: en-us Accept: / Connection: Keep-Alive Referer: 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; charset=utf-8 Content-Length: 285 Response headers Content-Type: text/html; charset=iso-8859-1 Content-Length: 1425 com.google.gwt.user.client.rpc.StatusCodeException: 404 HTTP ERROR: 404 NOT_FOUND RequestURI=/com.resume.Contacthandler.JUnit/GWT.rpc

From this output I am assuming something on the server side with Guice is not getting setup.

How do you setup the server side Guice servlets when running GWTTestCases ?

解决方案

There are much simpler ways to get Guice and GWT working other than the approach in the stuffthathappens blog. For instance, the following code is most of what you'd need to do to get a servlet up and running. This doesn't touch any GWT code so it's easy to test with pure JRE tests - you'd just need to set up a test injector and get an instance of the 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
}

这篇关于GWT RPC GWTTestCase + GUICE 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆