問題描述
假設我有以下偽類測試代碼:
Let's say i have the following psuedo like test code:
//Let's import Mockito statically so that the code looks clearer
import static org.mockito.Mockito.*;
//mock creation
List mockedList = mock(List.class);
//using mock object
mockedList.add("one");
mockedList.clear();
//what do these two verify methods do ?
verify(mockedList).add("one");
verify(mockedList).clear();
我一直顯示測試通過但我不知道驗證是什么意思?它到底在驗證什么?我知道我模擬了一個 add 和 clear 調用,但是這兩個 verify 調用是做什么的?
I keep showing the test passed but i dont know what the verify means ? what is it verifying exactly ? I understand that i mocked a call to add and clear but what does the two verify calls do ?
推薦答案
Mockito.verify(MockedObject).someMethodOnTheObject(someParametersToTheMethod);
驗證您在模擬對象上調用的方法確實被調用.如果沒有調用它們,或者使用錯誤的參數調用它們,或者調用錯誤的次數,它們將無法通過您的測試.
Mockito.verify(MockedObject).someMethodOnTheObject(someParametersToTheMethod);
verifies that the methods you called on your mocked object are indeed called. If they weren't called, or called with the wrong parameters, or called the wrong number of times, they would fail your test.
這篇關于Mockito - 驗證方法有什么作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!