2014-02-21 101 views
0

我想用GwtTestCase來測試我的gwt項目。
但我不能創建一個Composite組件的實例。如何使用GwtTestCase使用ui binder測試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粘合劑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 TesC ASE)

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代碼也正常工作。

回答

1

您有配置問題:

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_8GWTTestCase用途Firefox 3(GWT 2.6.0中的Firefox 17)仿真。如果您不關心Firefox(無論出於何種原因),或者只是想使用IE仿真運行測試,則通過-runStyle HtmlUnit:IE8(有效值爲FF2,FF3,IE6,IE7和IE8,直到GWT 2.5.1和FF17 ,IE8,IE9和Chrome在GWT 2.6,你可以通過用逗號分隔值:在gwt.args系統屬性-runStyle HtmlUnit:IE6,IE8)(注:在Eclipse中,在參數設置該屬性選項卡將覆蓋從GWT選項卡中的選項,所以你必須在那裏設置它們,例如-Dgwt.args="-war www-test -runStyle HtmlUnit:IE8"

+0

我在'gwt模塊中用''試了一下。但仍然是同樣的錯誤。 firefox的Dgwt.args是什麼? –