使用GWT-TestCase和GAE测试RPC调用的示例

时间:2009-06-09 20:56:06

标签: google-app-engine testing gwt

对于很多缩略词来说,这是怎么回事!

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

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分:1)

我得到了这个工作。这个答案假设你正在使用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;
        }
    }
}

我希望有所帮助。