【发布时间】:2016-06-01 16:44:44
【问题描述】:
我有这个包含接口的java代码。
ILocation<ILocation> contactLocations = fileSystem.getContactLocations();
for (ILocation location : contactLocations) {
location.getType();
...
}
public interface ILocation{
String getName();
...
}
public interface ILocationS<T extends ILocation> extends IListCollectionWrapper<T> {
...
}
public abstract interface IListCollectionWrapper<E>extends ICollectionWrapper<E>{
public abstract List<E> getAsList();
}
ILocation<ILocation> contactLocations = fileSystem.getContactLocations();
for (ILocation locations : contactLocations) {
...
}
我使用 Mockito 并尝试测试 for 循环的内容。
Iterator<ILocation> mockIterator = mock(Iterator.class);
Mockito.when(locations.iterator()).thenReturn(mockIterator);
Mockito.when(mockIterator.next()).thenReturn(location);
Mockito.when(location.getType()).thenReturn("test");
我从不进入 de for 循环
【问题讨论】:
-
您是否编写了自己的迭代器?如果是这样,您可以分享该代码吗?如果没有,您能否模拟
fileSystem.getContactLocations()以返回正确的ILocation对象,这样您就不必模拟迭代器?