2011-12-11 59 views

回答

2

谷歌推出了一個great article關於使用不同的測試方法與GWT。絕對檢查出來。我個人使用JUnit來測試後端的業務邏輯,Selenium用於從瀏覽器的角度測試整個UI和應用程序。

+0

當我問這個問題後,我發現我可以使用Mockito進行演示者單元測試。但我仍然無法測試視圖,因爲我也使用SmartGwt,並且似乎我無法在單元測試中創建任何smartgwt對象。你能給我一些關於Selenium測試的細節嗎?謝謝。 – John

+0

查看OnGWT的「Selenium」標籤@ http://www.ongwt.com/tag/Selenium,很多很好的信息。 –

+0

非常感謝。 – John

3

演示者可以使用Jukito輕鬆進行單元測試。以下是一個使用Jukito進行測試的演示者的簡單示例。

@RunWith(JukitoRunner.class) 
public class ShowCommentsPresenterTest { 
    @Inject 
    private ShowCommentsPresenter showCommentsPresenter; 

    @Inject 
    private PlaceManager placeManager; 

    @Test 
    public void onReset_PlaceRequestHasNoShowId_ShouldHideView() { 
     //given 
     when(placeManager.getCurrentPlaceRequest()).thenReturn(new PlaceRequest()); 

     //when 
     showCommentsPresenter.onReset(); 

     //then 
     verify(showCommentsPresenter.getView()).hide(); 
    } 

    @Test 
    public void onReset_PlaceRequestHasAShowId_ShouldDisplayView() { 
     //given 
     String someShowId = "12345"; 
     when(placeManager.getCurrentPlaceRequest()).thenReturn(new PlaceRequest() 
      .with(ParameterTokens.getShowId(), someShowId)); 

     //when 
     showCommentsPresenter.onReset(); 

     //then 
     verify(showCommentsPresenter.getView()).display(); 
    } 
} 

在GWTP的理念中,視圖不應該直接進行單元測試。使用愚蠢的View作爲Presenter的奴隸,大多數邏輯可以通過演示者上的單元測試來測試。像Selenium這樣的工具更適合測試UI交互性。