如何使用 GwtTestCase 使用 UI 绑定器测试 gwt 复合组件



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

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

登录.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 binder xml content)

<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>

登录测试.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 绑定程序的类。
GWT.create(...)和原生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;您可以在 gwt.args 系统属性中传递多个逗号分隔值:-runStyle HtmlUnit:IE6,IE8 )(注意: 在 Eclipse 中,在"参数"选项卡中设置此属性会覆盖 GWT 选项卡中的选项,因此您也必须在那里设置它们,例如 -Dgwt.args="-war www-test -runStyle HtmlUnit:IE8"