創建嘲笑名單我想創建一個嘲笑列表來測試下面的代碼:通過的Mockito
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!
我該如何解決?謝謝
可能重複[的Mockito:嘲諷,這將在for循環中被循環一個ArrayList(HTTP://計算器。 com/questions/18483176/mockito-mocking-an-arraylist-that-will-be-loop-in-a-for-loop) –