In this situation, the connection between the model and the presenter is fairly free (passing through the action listener), which is probably why you should not use the model layout.
You can use the real model (I would prefer that if the real model is simple enough), or, as I said in the fragment below, make a stub inside the test code.
@RunWith(MockitoJUnitRunner.class) public class PresenterTest { @Mock IView view; IModel model; @Before public void setup() { model = new StubModel(); new Presenter(model, view); } @Test public void presenterUpdatesViewWhenModelChanges() { model.setText("Test Text"); verify(view).setText("Test Text"); } private class StubModel implements IModel { private String text; private List<ActionListener> actionListeners; StubModel() { actionListeners = new ArrayList<ActionListener>(); } @Override public void setText(String text) { this.text = text; whenModelChanges(); } @Override public String getText() { return text; } @Override public void whenModelChanges() { for (ActionListener listener: actionListeners) { listener.actionPerformed(null); } } @Override public void addModelChangesListener(AbstractAction action) { actionListeners.add(action); } } }
You may be able to customize this test using a mock model on which you set up stub calls, but for this reasonably, you may also need a mock action that will complicate the situation, as the action is created by the facilitator.
This looks like a lot of test code for testing essentially one line of code in a presenter class, but the biggest snippet is a stub model, which can be replaced with a real model or retrieved from this test class and other tests are shared.
source share