Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The idea behind unit testing of presenter is to mock the view, and write several assertions against state of this mocked view, which would be represented visually in the real life app. Thanks to such an approach there is no need for running full <code>GWTTestCase</code>, which takes a lot of time and should be rather put in the category of integration testing, not unit testing.</p> <p>If you would try both MVP approaches, the unit tests could look like:</p> <p>MVP 1:</p> <pre><code>@Test public void shouldAddContactOnAddContactClicked() { // given ContactsPresenter.Display display = mock(ContactsPresenter.Display.class); MockButton addButton = new MockButton(); given(display.getAddButton()).willReturn(addButton); ContactsDisplay.Presenter presenter = new ContactsPresenter(); presenter.bindView(display); presenter.setContacts(new ArrayList&lt;Contact&gt;()); // when addButton.click(); // then verify(display).addContact(any()); assertThat(presenter.getContacts().size(), is(1)); } </code></pre> <p>Where the <code>MockButton</code> is something I described here: <a href="https://stackoverflow.com/questions/3417551/comprehensive-pros-cons-of-mocking-frameworks-for-gwt/3428673#3428673">Comprehensive Pros/Cons of Mocking Frameworks for GWT</a></p> <p>Although possible, it is not really convenient to mock things this way. The MVP2 approach seems to perform better:</p> <pre><code>@Test public void shouldAddContactOnAddContactClicked() { // given ContactsView view = mock(ContactsView.class); ContactsView.Presenter presenter = new ContactsPresenter(); presenter.bindView(view); // assuming that presenter will call view.setPresenter(this) presenter.setContacts(new ArrayList&lt;Contact&gt;()); // when presenter.onAddContactClicked(); // then verify(view).addContact(any()); assertThat(presenter.getContacts().size(), is(1)); } </code></pre> <p>Another reason for using the second approach is the problem in MVP1 with declaring elements of the display, which would trigger different events (e.g. ClickEvent, FocusEvent). MVP2 also makes things easier when it comes to <code>UiBinder</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload