2009-06-09 64 views
2

这对于大量首字母缩略词来说如何?使用带GAE的GWT-TestCase测试RPC调用的示例

我在使用GWT的GWTTestCase测试GWT的RPC机制时遇到了问题。我使用GWT中包含的junitCreator工具创建了一个测试类。我正在尝试使用由junitCreator创建的创建的“托管模式”测试配置文件使用内置的Google App Engine进行测试。当我运行测试,我不断收到错误,说这样的话

Starting HTTP on port 0 
    HTTP listening on port 49569 
The development shell servlet received a request for 'greet' in module 'com.google.gwt.sample.stockwatcher.StockWatcher.JUnit.gwt.xml' 
    [WARN] Resource not found: greet; (could a file be missing from the public path or a <servlet> tag misconfigured in module com.google.gwt.sample.stockwatcher.StockWatcher.JUnit.gwt.xml ?) 
com.google.gwt.user.client.rpc.StatusCodeException: Cannot find resource 'greet' in the public path of module 'com.google.gwt.sample.stockwatcher.StockWatcher.JUnit' 

我希望有人的地方已成功运行JUnit测试(使用GWTTestCase或只是简单的TestCase),将允许GWT RPC的测试。如果是这种情况,你可以提一下你采取的步骤,或者更好的办法,只是发布有用的代码。谢谢。

+0

我遇到同样的问题。希望我能够提供一个答案。 – 2010-02-14 19:24:42

回答

1

SyncProxy允许您从Java进行GWT RPC调用。所以,你可以测试你的GWT RPC与常规的测试用例(和速度比GwtTestcase)


http://www.gdevelop.com/w/blog/2010/01/10/testing-gwt-rpc-services/
http://www.gdevelop.com/w/blog/2010/03/13/invoke-gwt-rpc-services-deployed-on-google-app-engine/

+0

男人,如果像这样的东西被添加到主要开发下载包将是很好的。谢谢。 – 2011-01-22 23:06:15

+0

由于上述链接显示已经失效,请参阅项目主站点:https://code.google.com/p/gwt-syncproxy/ – JCricket 2013-12-03 08:51:23

1

我得到了这个工作。这个答案假设你在使用Gradle,但是这很容易被用来从蚂蚁运行。首先,您必须确保将您的GWT测试与常规JUnit测试分开。我为常规测试创建了“tests/standalone”,为我的GWT测试创建了“tests/gwt”。我最终还是得到了一份包含所有信息的HTML报告。

接下来,你需要确保JUnit是您的蚁类路径的一部分,如下所述:

http://gradle.1045684.n5.nabble.com/Calling-ant-test-target-fails-with-junit-classpath-issue-newbie-td4385167.html

然后,使用类似这样的东西编译GWT测试和运行它们:

task gwtTestCompile(dependsOn: [compileJava]) << { 
    ant.echo("Copy the test sources in so they're part of the source..."); 
    copy { 
     from "tests/gwt" 
     into "$buildDir/src" 
    } 
    gwtTestBuildDir = "$buildDir/classes/test-gwt"; 
    (new File(gwtTestBuildDir)).mkdirs() 
    (new File("$buildDir/test-results")).mkdirs() 

    ant.echo("Compile the tests..."); 
    ant.javac(srcdir: "tests/gwt", destdir: gwtTestBuildDir) { 
     classpath { 
      pathElement(location: "$buildDir/src") 
      pathElement(location: "$buildDir/classes/main") 
      pathElement(path: configurations.runtime.asPath) 
      pathElement(path: configurations.testCompile.asPath) 
      pathElement(path: configurations.gwt.asPath) 
      pathElement(path: configurations.gwtSources.asPath) 
     } 
    } 

    ant.echo("Run the tests..."); 
    ant.junit(haltonfailure: "true", fork: "true") { 
     classpath { 
      pathElement(location: "$buildDir/src") 
      pathElement(location: "$buildDir/classes/main") 
      pathElement(location: gwtTestBuildDir) 
      pathElement(path: configurations.runtime.asPath) 
      pathElement(path: configurations.testCompile.asPath) 
      pathElement(path: configurations.gwt.asPath) 
      pathElement(path: configurations.gwtSources.asPath) 
     } 
     jvmarg(value: "-Xmx512m") 
     jvmarg(line: "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005") 
     test(name: "com.onlyinsight.client.LoginTest", todir: "$buildDir/test-results") 
     formatter(type: "xml") 
    } 
} 

test.dependsOn(gwtTestCompile); 

最后,这里有一个简单的GWT测试:

public class LoginTest extends GWTTestCase 
{ 
    public String getModuleName() 
    { 
     return "com.onlyinsight.ConfModule"; 
    } 

    public void testRealUserLogin() 
    { 
     UserServiceAsync userService = UserService.App.getInstance(); 

     userService.login("a", "a", new AsyncCallback<User>() 
     { 
      public void onFailure(Throwable caught) 
      { 
       throw new RuntimeException("Unexpected Exception occurred.", caught); 
      } 

      public void onSuccess(User user) 
      { 
       assertEquals("a", user.getUserName()); 
       assertEquals("a", user.getPassword()); 
       assertEquals(UserRole.Administrator, user.getRole()); 
       assertEquals("Test", user.getFirstName()); 
       assertEquals("User", user.getLastName()); 
       assertEquals("[email protected]", user.getEmail()); 

       // Okay, now this test case can finish. 
       finishTest(); 
      } 
     }); 

     // Tell JUnit not to quit the test, so it allows the asynchronous method above to run. 
     delayTestFinish(10 * 1000); 
    } 
} 

如果您的RPC实例没有一个方便的getInstance()方法,然后添加一个:

public interface UserService extends RemoteService { 

    public User login(String username, String password) throws NotLoggedInException; 

    public String getLoginURL(OAuthProviderEnum provider) throws NotLoggedInException; 

    public User loginWithOAuth(OAuthProviderEnum provider, String email, String authToken) throws NotLoggedInException; 

    /** 
    * Utility/Convenience class. 
    * Use UserService.App.getInstance() to access static instance of UserServiceAsync 
    */ 
    public static class App { 
     private static final UserServiceAsync ourInstance = (UserServiceAsync) GWT.create(UserService.class); 

     public static UserServiceAsync getInstance() 
     { 
      return ourInstance; 
     } 
    } 
} 

我希望帮助。

+0

我建议您将all-in-one gwtTestCompile任务分为两个不同的任务类型编译和测试。这会使Gradle每次都不会重新编译测试。 – 2012-03-12 11:15:29