2012-09-26 50 views
1

我使用標準的GWT示例創建了一個新的Web應用程序項目。然後我想用下面的測試類來測試greetingserviceimpl。我不知道問題在哪裏。我還上傳項目:http://ul.to/1pz1989yGWT SyncProxy測試

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」
[錯誤]提示:以前的編譯器錯誤可能使這一類型的不可用
[錯誤]提示:請檢查您的模塊繼承鏈;它可能不會繼承所需的模塊,或者模塊可能沒有正確添加源路徑條目

回答

0

您的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' 
    } 
} 

編輯:

測試類 'de.RPCTest' 中未找到模塊 'de.GreetingTest';該類型沒有編譯單元被看作

就像你經常GWT代碼必須在client包,所以必須在GWTTestCase代碼 - 這也被運行如JavaScript這樣好像是在能夠正確地進行測試一個瀏覽器。根據錯誤,我猜你的EntryPoint等在de.client - 這個測試也應該在那裏。

+0

謝謝。但我有另一個問題。有可能管理客戶端會話嗎?因爲服務器端想知道在HttpSession對象中保存爲String對象的用戶名。 – user1701135

+0

我真的不知道你甚至在這種情況下單元測試 - servlet嗎?如果是這樣,只需創建一個正常的測試用例並創建一個servlet的副本? GWT-RPC本身?我非常肯定這是有效的......通過測試servlet,你可以設置你想要的任何其他模擬會話,甚至不需要處理GWT端。但答案確實是否定的:GWTTestCases運行一個特殊的Jetty實例,你不能干涉太多,因爲這不是這些測試的目的。 –

+0

上述解決方案不起作用。這可能是我的錯。你試過這個嗎?我認爲一個可以導入的GWTTestCases工作項目會對我有很大的幫助。也許你可以幫助我。 – user1701135