How to test GWT/GWTP project?
-
13-03-2021 - |
Question
I am currently building a web application using GWT, GWTP.
And I have some questions about testings:
- Is there a Lint-like tool for GWTP or GWT?
- How to test presenters? (GWTP with Mockito)
- How to test views?
Thanks.
Solution
Google put out a great article about using different testing methodologies with GWT. Definitely check it out. Personally, I use JUnit when I'm testing back-end stuff like business logic, and Selenium for testing the UI and application as a whole from the browser's perspective.
OTHER TIPS
Presenters can be easily unit-tested using Jukito. Here's a quick example of a Presenter being tested using 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();
}
}
In GWTP's philosophy, Views should not be unit-tested directly. Using a dumb View that is a slave to the Presenter, most of the logic can be tested through unit tests on the Presenters. Tools like Selenium are a better fit for testing UI interactivity.