tags:

views:

141

answers:

3

Hello,

i dunno if the question is already ask, but i couldn't find it... i'm searching a way to mock my view in order to test my presenter ? i try to use mockito for the view, and set it in the presenter, but in result in presenter, when i call presenter.getDisplay() (the getter for the view) all of my widget is null ? as i believe it's normal mockito will not mock the widget.

i'm 100% sure i mistaken something but i couldnt find it.

thanks for your enlightement :)

A: 

you need to make sure that you told mockito to return the mocked view when you call getDislay().

Sth like when(presenter.getDisplay()).thenReturn(mockView);

markovuksanovic
indeed but should i mock every widget one by one into the view ? mock every widgetmock the view and tell mockito to return widget when ask ?
Yannick Eurin
Your view should not return widgets -- it should have methods which do something to the widgets (e.g. show/hide, set a string in a label, get a string from an input), but your Presenter should *not* know that the Display contains widgets
tdavies
when you use mock(yourClass.class) you get a reference to a object that doesn't know how to anything. You need to tell mockito what you expect to happen after a call to each method.
markovuksanovic
one more thing - tdavies is right - your presenter should not do anything to the widgets. View is the object that is supposed to manipulate widgets. You just call methods on your view object (for example (myView.show())) and then the view object handles manipulating widgets.
markovuksanovic
I seem to have misunderstood your question. No you should NOT mock every widget. The only thing you should mock is the view. Then you tell the mockito what you expect to happen when some method in the view object is called. This way your tests will be totally independent of the view implementation. You will be able to replace the view (for example you decided that you want to use some other windows from some other library) implementation and still have the tests valid.
markovuksanovic
+1  A: 

Here's a very simple working example:

import junit.framework.TestCase;
import org.mockito.Mockito;

public class SimpleGwtTest extends TestCase {
    private static class UpperCasePresenter {
        private final Display display;

        public interface Display {
            void setString(String s);
        }

        private UpperCasePresenter(String s, Display display) {
            this.display = display;
            display.setString(s.toUpperCase());
        }
    }

    public void testPresenter() {
        UpperCasePresenter.Display d = Mockito.mock(UpperCasePresenter.Display.class);
        new UpperCasePresenter("foo", d);
        Mockito.verify(d).setString("FOO");
    }
}

Of course normally your Presenter wouldn't be inside your test case.

tdavies
A: 

With MVP Presenter depends on View class (Display) via gwt interfaces, such as HasValue, HasHTML, HasClickHandlers, etc. and possibly new interfaces as necessary. Presenter classes should use these interfaces instead of widget classes directly. Therefore Mocking View interfaces is rather simple and should be shared across test methods (using setUp or @Before). This should also include mocking GWT infrastructure such as EventBus, etc.

For nice blog with examples with EasyMock (easy but not straight forward to convert to mockito) see here.

grigory