【问题标题】:Example of testing a RPC call using GWT-TestCase with GAE使用 GWT-TestCase 和 GAE 测试 RPC 调用的示例
【发布时间】:2009-06-09 20:56:05
【问题描述】:

很多首字母缩略词是怎么回事!

我在使用 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。如果是这种情况,您能否提及您采取的步骤,或者更好的是,只需发布​​有效的代码。谢谢。

【问题讨论】:

  • 我遇到了同样的问题。希望我能提供答案。

标签: google-app-engine testing gwt


【解决方案1】:

SyncProxy 允许您从 Java 进行 GWT RPC 调用。因此,您可以使用常规 Testcase(并且比 GwtTestcase 更快)测试您的 GWT RPC


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/

【讨论】:

  • 伙计,如果这样的东西被添加到主要的开发下载包中会很好。谢谢。
  • 由于以上链接失效,请查看项目主站:code.google.com/p/gwt-syncproxy
【解决方案2】:

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

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

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("canada@onlyinsight.com", 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;
        }
    }
}

希望对你有帮助。

【讨论】:

  • 我建议您将一体式 gwtTestCompile 任务拆分为 Compile 和 Test 类型的两个不同任务。这将防止 gradle 每次都重新编译测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多