我想创建一个模拟列表来测试下面的代码:
for (String history : list) {
//code here
}
这是我的实施:
public static List<String> createList(List<String> mockedList) {
List<String> list = mock(List.class);
Iterator<String> iterHistory = mock(Iterator.class);
OngoingStubbing<Boolean> osBoolean = when(iterHistory.hasNext());
OngoingStubbing<String> osHistory = when(iterHistory.next());
for (String history : mockedList) {
osBoolean = osBoolean.thenReturn(true);
osHistory = osHistory.thenReturn(history);
}
osBoolean = osBoolean.thenReturn(false);
when(list.iterator()).thenReturn(iterHistory);
return list;
}
但是当测试运行时,它会在行中抛出异常:
OngoingStubbing<DyActionHistory> osHistory = when(iterHistory.next());
了解详情:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
我该如何解决?感谢
答案 0 :(得分:28)
好的,这是一件坏事。不要嘲笑一个清单;相反,模拟列表中的各个对象。有关如何执行此操作,请参阅Mockito: mocking an arraylist that will be looped in a for loop。
另外,你为什么使用PowerMock?您似乎没有做任何需要PowerMock的事情。
但问题的真正原因是,在完成存根之前,您正在两个不同的对象上使用when
。当你调用when
并提供你试图存根的方法调用时,你在Mockito或PowerMock中做的下一件事就是指定在调用该方法时会发生什么 - 也就是说, thenReturn
部分。在您再次拨打when
之前,每次拨打thenReturn
后,必须先拨打一次when
来拨打when
。您拨打thenReturn
两次电话而未致电{{1}} - 这是您的错误。
答案 1 :(得分:7)
在处理模拟列表并迭代它们时,我总是使用类似的东西:
@Spy
private List<Object> parts = new ArrayList<>();
答案 2 :(得分:4)
我们可以为foreach循环正确地模拟列表。请在下面找到代码段和解释。
这是我的实际类方法,我想通过模拟列表创建测试用例。
this.nameList
是一个列表对象。
public void setOptions(){
// ....
for (String str : this.nameList) {
str = "-"+str;
}
// ....
}
foreach循环内部在迭代器上工作,所以这里我们创建了迭代器的模拟。
Mockito框架具有使用Mockito.when().thenReturn()
在特定方法调用上返回值对的功能,即在hasNext()
上我们传递1st true而在第二次调用false时,因此我们的循环将仅继续两次。在next()
,我们只返回实际的返回值。
@Test
public void testSetOptions(){
// ...
Iterator<SampleFilter> itr = Mockito.mock(Iterator.class);
Mockito.when(itr.hasNext()).thenReturn(true, false);
Mockito.when(itr.next()).thenReturn(Mockito.any(String.class);
List mockNameList = Mockito.mock(List.class);
Mockito.when(mockNameList.iterator()).thenReturn(itr);
// ...
}
通过这种方式,我们可以避免使用list of mock发送实际列表进行测试。