問題描述
Mockito - 我理解間諜調用對象的真實方法,而模擬調用雙重對象的方法.除非有代碼氣味,否則還要避免間諜.但是,間諜是如何工作的,我應該什么時候真正使用它們?它們與模擬有何不同?
Mockito - I understand a spy calls the real methods on an object, while a mock calls methods on the double object. Also spies are to be avoided unless there is a code smell. However, how do spies work and when should i actually use them? How are they different from mocks?
推薦答案
從技術上講,模擬"和間諜"都是一種特殊的測試替身".
Technically speaking both "mocks" and "spies" are a special kind of "test doubles".
不幸的是,Mockito 使這種區別變得很奇怪.
Mockito is unfortunately making the distinction weird.
mockito 中的模擬是其他模擬框架中的普通模擬(允許您存根調用;也就是說,從方法調用中返回特定值).
A mock in mockito is a normal mock in other mocking frameworks (allows you to stub invocations; that is, return specific values out of method calls).
mockito 中的間諜是其他模擬框架中的部分模擬(部分對象將被模擬,部分將使用真實的方法調用).
A spy in mockito is a partial mock in other mocking frameworks (part of the object will be mocked and part will use real method invocations).
這篇關于Mockito - @Spy vs @Mock的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!