GWT 同步代理测试

标签 gwt testing junit rpc

我使用标准 GWT 示例创建了一个新的 Web 应用程序项目。然后我想用下面的测试类来测试 greetingserviceimpl。我不知道问题出在哪里。我还上传了项目:http://ul.to/1pz1989y

public class RPCTest extends GWTTestCase {

@Override
public String getModuleName() {
    // TODO Auto-generated method stub
    return "de.GreetingTest";
}

public void testGreetingAsync() {
    GreetingServiceAsync rpcService =  (GreetingServiceAsync) SyncProxy.newProxyInstance(GreetingServiceAsync.class,"http://127.0.0.1:8888/GreetingTest.html?gwt.codesvr=127.0.0.1:9997");
    rpcService.greetServer("GWT User", new AsyncCallback<String>() {
        public void onFailure(Throwable ex) {                
            ex.printStackTrace();
            fail(ex.getMessage());              
        }
        public void onSuccess(String result) {
            assertNotNull(result);               
            finishTest();//
        }
    });
    delayTestFinish(1000);
}

验证新编译的单元 在第一遍中忽略了 1 个有编译错误的单元。
使用 -strict 或 -logLevel 设置为 TRACE 或 DEBUG 进行编译以查看所有错误。
[错误] 第 17 行:没有可用于类型 com.gdevelop.gwt.syncrpc.SyncProxy 的源代码;您是否忘记继承所需的模块?
[错误] 无法找到类型“de.client.RPCTest”
[错误] 提示:以前的编译器错误可能导致此类型不可用
[错误] 提示:检查模块的继承链;它可能没有继承所需的模块,或者模块可能没有正确添加其源路径条目

最佳答案

您的 rpc 服务是异步的 - 它没有在 testGreetingAsync 方法返回时完成。 GWTTestCase(但您正在扩展 TestCase,您可能应该更改它)支持此方法 - 在方法末尾调用 delayTestFinish 以指示测试是异步的。然后,在成功后调用 finishTest

public class RPCtest extends GWTTestCase {
    public void testGreetingAsync() {
        GreetingServiceAsync rpcService =  (GreetingServiceAsync) SyncProxy.newProxyInstance(GreetingServiceAsync.class,"http://127.0.0.1:8888/Tests.html?gwt.codesvr=127.0.0.1:9997");
        rpcService.greetServer("GWT User", new AsyncCallback() {
            public void onFailure(Throwable ex) {
                //indicate that a failure has occured
                ex.printStackTrace();
                fail(ex.getMessage());//something like this               
            }
            public void onSuccess(Object result) {
                //verify the value...
                assertNotNull(result);

                //Then, once sure the value is good, finish the test
                finishTest();//This tells GWTTestCase that the async part is done
            }
        });
        delayTestFinish(1000);//1000 means 'delay for 1 second, after that time out'
    }
}

编辑更新的问题:

The test class 'de.RPCTest' was not found in module 'de.GreetingTest'; no compilation unit for that type was seen

就像您的常规 GWT 代码必须位于 client 包中一样,您的 GWTTestCase 代码也必须如此 - 这也作为 JavaScript 运行,因此它可以像在浏览器中一样被正确测试。根据错误,我猜你的 EntryPoint 等在 de.client 中 - 这个测试也应该在那里。

关于GWT 同步代理测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608256/

相关文章:

java - Junit 测试 - Jackson toString 中的 Eclemma 覆盖率

GWT Datagrid 不显示数据,但包含它

带参数的 Django url,不知道如何测试

java - 在 Mojo 的 gwt-maven-plugin 中配置 AppWidgetset.gwt.xml 的位置

testing - Grails 测试目录的相对路径

http - 如何测试 cyclejs http 驱动程序?

java - DBunit 中的 IDataSet 操作

java - 使用 SpringRunner 进行单元测试并得到 jdbc 错误

具有命令模式的 GWT RequestFactory

java - GWT 类成员命名