java - 如何使用 GwtTestCase 测试带有 ui 绑定(bind)器的 gwt 复合组件?

标签 java gwt junit uibinder gwttestcase

我正在尝试使用 GwtTestCase 测试我的 gwt 项目。
但我无法创建复合组件的实例。

这是代码和堆栈跟踪...

Login.java(我的复合组件)

public class Login extends Composite{  
    @UiField    
    TextBox userName;  

    @UiField  
    TextBox password;

    @UiField  
    Button loginButton;  

    private static LoginUiBinder uiBinder = GWT.create(LoginUiBinder.class);  

    interface LoginUiBinder extends UiBinder<Widget, Login> {}      

    public Login() {  
        initWidget(uiBinder.createAndBindUi(this));  
        loginButton.addClickHandler(new ClickHandler() {  

        @Override  
            public void onClick(ClickEvent event) {
                Window.alert("clicked");            
            }
        });  
    }  
}  

Login.ui.xml(ui 绑定(bind)器 xml 内容)

<g:HTMLPanel>  
    <div>  
        <g:Label text="username:"></g:Label>  
        <g:TextBox ui:field="userName"></g:TextBox>  
    </div>  
    <div>  
        <g:Label text="password:"></g:Label>  
        <g:TextBox ui:field="password"></g:TextBox>  
    </div>      
    <g:Button ui:field="loginButton" text="Login"></g:Button>  
</g:HTMLPanel>

LoginTest.java(这是我的 Gwt TesCase)

public class SampleGwtTestCase extends GWTTestCase{  
    public void test01(){  
        Login login = new Login();  
    }  

    @Override  
    public String getModuleName() {
        return "com.mycompany.TestingGwt";  
    }
}

错误堆栈跟踪

java.lang.RuntimeException: Remote test failed at 192.168.2.15 / Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19
    at com.google.gwt.junit.JUnitShell.processTestResult(JUnitShell.java:1283)
    at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1403)
    at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1304)
    at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:652)
    at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:441)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:296)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: java.lang.Exception: com.google.gwt.core.client.JavaScriptException: (null) @com.google.gwt.dom.client.DOMImplTrident::isOrHasChildImpl(Lcom/google/gwt/dom/client/Node;Lcom/google/gwt/dom/client/Node;)([JavaScript object(22), JavaScript object(26)]): null
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeBoolean(ModuleSpace.java:194)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeBoolean(JavaScriptHost.java:35)
    at com.google.gwt.dom.client.DOMImplTrident.isOrHasChildImpl(DOMImplTrident.java)
    at com.google.gwt.dom.client.DOMImplTrident.isOrHasChild(DOMImplTrident.java:284)
    at com.google.gwt.dom.client.Node$.isOrHasChild$(Node.java:280)
    at com.google.gwt.user.client.ui.HTMLPanel.addAndReplaceElement(HTMLPanel.java:220)
    at com.google.gwt.user.client.ui.HTMLPanel.addAndReplaceElement(HTMLPanel.java:190)
    at com.mycompany.client.login.Login_LoginUiBinderImpl$Widgets.build_f_HTMLPanel1(Login_LoginUiBinderImpl.java:95)
    at com.mycompany.client.login.Login_LoginUiBinderImpl$Widgets.get_f_HTMLPanel1(Login_LoginUiBinderImpl.java:77)
    at com.mycompany.client.login.Login_LoginUiBinderImpl$Widgets.access$0(Login_LoginUiBinderImpl.java:76)
    at com.mycompany.client.login.Login_LoginUiBinderImpl.createAndBindUi(Login_LoginUiBinderImpl.java:30)
    at com.mycompany.client.login.Login_LoginUiBinderImpl.createAndBindUi(Login_LoginUiBinderImpl.java:1)
    at com.mycompany.client.login.Login.<init>(Login.java:50)
    at com.mycompany.client.login.SampleGwtTestCase.test01(SampleGwtTestCase.java:11)
    at com.mycompany.client.login.__SampleGwtTestCase_unitTestImpl.doRunTest(__SampleGwtTestCase_unitTestImpl.java:7)
    at junit.framework.TestCase.runTest(TestCase.java:62)
    at com.google.gwt.junit.client.GWTTestCase.runBare(GWTTestCase.java:188)
    at com.google.gwt.junit.client.GWTTestCase.__doRunTest(GWTTestCase.java:129)
    at com.google.gwt.junit.client.impl.GWTRunner.runTest(GWTRunner.java:390)
    at com.google.gwt.junit.client.impl.GWTRunner.doRunTest(GWTRunner.java:318)
    at com.google.gwt.junit.client.impl.GWTRunner.access$9(GWTRunner.java:312)
    at com.google.gwt.junit.client.impl.GWTRunner$TestBlockListener.onSuccess(GWTRunner.java:107)
    at com.google.gwt.junit.client.impl.GWTRunner$InitialResponseListener.onSuccess(GWTRunner.java:63)
    at com.google.gwt.junit.client.impl.GWTRunner$InitialResponseListener.onSuccess(GWTRunner.java:1)
    at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:232)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:258)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:412)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:249)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Unknown Source)

我的测试正确吗?
配置上还需要什么吗? 对于没有 ui 绑定(bind)程序的类,测试用例可以成功运行。
GWT.create(...) 和 native JavaScript 代码也可以正常工作。

最佳答案

您遇到配置问题:

java.lang.RuntimeException: Remote test failed at 192.168.2.15 / Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19

Caused by: java.lang.Exception: com.google.gwt.core.client.JavaScriptException: (null) @com.google.gwt.dom.client.DOMImplTrident::isOrHasChildImpl(Lcom/google/gwt/dom/client/Node;Lcom/google/gwt/dom/client/Node;)([JavaScript object(22), JavaScript object(26)]): null

您似乎已将 user.agent 属性修复为排除 gecko1_8 的某些值,并且 GWTTestCase 使用 Firefox 3 (GWT 2.6.0 中的 Firefox 17)模拟。如果您不关心 Firefox(无论出于何种原因),或者只是想使用 IE 模拟运行测试,请传递 -runStyle HtmlUnit:IE8 (有效值为 FF2、FF3、IE6、IE7和 IE8 直至 GWT 2.5.1,以及 GWT 2.6 中的 FF17、IE8、IE9 和 Chrome;您可以在 中传递多个逗号分隔值:-runStyle HtmlUnit:IE6,IE8​​) gwt.args 系统属性(注意:在 Eclipse 中,在 Arguments 选项卡中设置此属性会覆盖 GWT 选项卡中的选项,因此您必须设置它们也有,例如 -Dgwt.args="-war www-test -runStyle HtmlUnit:IE8")

关于java - 如何使用 GwtTestCase 测试带有 ui 绑定(bind)器的 gwt 复合组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937576/

相关文章:

java - 通过jni将jint数组从c返回到java

java - PowerMockito.verifyStatic() 问题

java - gwt-maven-插件 : Only generate necessary js files

java - GXT 窗口隐藏后设置焦点

java - 测试 Hibernate DAO,无需围绕它构建宇宙

java - Junit Automation 测试中的某些测试需要许多不同的输入

java - 如何将列表的值传递给隐藏标签?

java - JAXB 设置自顶向下解码

java - 查找不相交集的数量

java - 如何在集成测试中结合仅客户端和仅服务器代码?