- 我在JUnit 测试中使用Mockito。我嘲笑了一个.
NavigableSet
- 在我的单元测试中,我需要循环到 this 中包含的对象
NavigableSet
。 NavigableSet
出于测试目的,我计划仅包含 1 个对象。
以下是我使用 Mockito 创建模拟对象的代码:
NavigableSet<ObjectToProcess> newMockedSet = new TreeSet<ObjectToProcess>();
ObjectToProcess mockedInnerObject = new ObjectToProcess(); // initialization of this object
newMockedSet.add(mockedInnerObject); // I just added the only 1 object contained in the NavigableSet
Mockito.when(getSetOfObjectToProcess()).thenReturn(newMockedSet);
为什么下面的while
循环给出了一个NavigableSet
只包含 1 个对象的无限循环?
NavigableSet<ObjectToProcess> anotherSetOfObjects = getSetOfObjectToProcess(); // retrieving the mocked object
while (anotherSetOfObjects.iterator().hasNext())
{
ObjectToProcess thisPaymentDemand = anotherSetOfObjects.iterator().next();
// do stuff
}
为什么以下for
循环的行为与预期一样只循环一次?
NavigableSet<ObjectToProcess> anotherSetOfObjects = getSetOfObjectToProcess(); // retrieving the mocked object
for (ObjectToProcess thisMockedInnerObject : anotherSetOfObjects)
{
ObjectToProcess thisPaymentDemand = thisMockedInnerObject;
// do stuff
}