我试图熟悉TDD和Presenter First Pattern。现在我为我的Presenter.class写了一个测试用例。我的目标是覆盖整个Presenter.class,包括动作事件,但我没有胶水如何使用Mockito。
Presenter.class:
public class Presenter {
IModel model;
IView view;
public Presenter(final IModel model, final IView view) {
this.model = model;
this.view = view;
this.model.addModelChangesListener(new AbstractAction() {
public void actionPerformed(ActionEvent arg0) {
view.setText(model.getText());
}
});
}}
IView.class:
public interface IView {
public void setText(String text);
}
IModel.class:
public interface IModel {
public void setText();
public String getText();
public void whenModelChanges();
public void addModelChangesListener(AbstractAction action);
}
PresenterTest.class:
@RunWith(MockitoJUnitRunner.class)
public class PresenterTest {
@Mock
IView view;
@Mock
IModel model;
@Before
public void setup() {
new Presenter(model, view);
}
@Test
public void test1() {
}
}
提前致谢!
答案 0 :(得分:1)
起初......谢谢你们!
过了一段时间我发现了这个解决方案并坚持下去,因为我不想在presenter类中实现任何接口,我也不想在我的测试中创建存根类。
IVIEW
public interface IView {
public void setText(String text);
}
IModel
public interface IModel {
public String getText();
public void addModelChangeListener(Action a);
}
演示
public class Presenter {
private IModel model;
private IView view;
public Presenter(final IModel model, final IView view) {
this.model = model;
this.view = view;
model.addModelChangeListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
view.setText(model.getText());
}
});
}
}
PresenterTest
@RunWith(MockitoJUnitRunner.class)
public class PresenterTest {
@Mock
IView view;
@Mock
IModel model;
@Test
public void when_model_changes_presenter_should_update_view() {
ArgumentCaptor<Action> event = ArgumentCaptor.forClass(Action.class);
when(model.getText()).thenReturn("test-string");
new Presenter(model, view);
verify(model).addModelChangeListener(event.capture());
event.getValue().actionPerformed(null);
verify(view).setText("test-string");
}
}
答案 1 :(得分:0)
在这种情况下,模型和演示者之间的连接足够松散(通过动作监听器进行通信),您可能最好不要使用模拟模型。
你可以使用一个真实的模型(如果真实的模型足够简单,我更喜欢它),或者正如我在下面的代码片段中所做的那样,在你的测试代码中创建一个存根。
@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);
}
}
}
您可以使用模拟模型设置此测试,您可以在其上设置存根调用,但为了明智地执行此操作,您可能还需要模拟操作,这会使操作复杂化,因为操作由演示者创建。
这似乎是很多测试代码,用于在您的演示者类中测试基本上一行代码,但最大的块是存根模型,可能被实际模型替换或从此测试类中提取并与之共享其他测试。
答案 2 :(得分:0)
这种情况下,一点点重构可以走很长的路。学会“聆听”测试并让他们推动设计。 Model
只需知道需要通知ActionListener
,而不关心它是AbstractAction
。使用类合同中可能的最小接口。以下是进行简单测试的重构(可能太简单了,不值得单元测试,但你明白了):
Presenter.class:
public class Presenter {
public Presenter(final IModel model, final IView v) implements ActionListener {
this.model = model;
this.view = v;
model.addModelChangesListener(this);
}
public void actionPerformed(ActionEvent arg0) {
view.setText(model.getText());
}
}
IModel.class:
public interface IModel {
public void addModelChangesListener(ActionListener action);
}
PresenterTest.class:
@RunWith(MockitoJUnitRunner.class)
public class PresenterTest {
@Mock IView view;
@Mock IModel model;
@Test
public void when_model_changes_presenter_should_update_text() {
when(model.getText()).thenReturn("Test Text");
Presenter p = new Presenter(model, view);
p.actionPerformed(null);
verify(view).setText("Test Text");
}
}