How do i use EasyMock to mock a basic listener. I have tried using a void technique described here and it did not work. My goal is to test the addModelListener() method. I want to add a listener and just make sure the listener gets called when i setData().
//The Listener
public interface AListener {
public void dataChanged();
}
//The class being tested.
public class TheModel {
private List<AListener> modelListeners;
public void addModelListener(AListener aListener) {
if (this.modelListeners == null) {
this.modelListeners = new ArrayList<AListener>();
}
this.modelListeners.add(aListener);
}
public void setData(Data data) {
this.data = data;
this.notifyDataChanged();
}
private void notifyDataChanged() {
if (this.modelListeners != null && !this.modelListeners.isEmpty()) {
for (AListener listener : this.modelListeners) {
if (listener != null) {
listener.dataChanged();
}
}
}
}
}
//The test. My initial attempts with the void stuff.
public void testAddModelListener() {
AListener listener = EasyMock.createNiceMock(AListener.class);
listener.dataChanged();
EasyMock.expectLastCall().times(1);
EasyMock.replay(listener);
TheModel model = new TheModel(null);
model.addModelListener(listener);
model.setData(null);
EasyMock.verify(listener);
}
I would expect the above test to verify how many times the listener.dataChanged() method was called. It fails saying it was never called.
Aucun commentaire:
Enregistrer un commentaire