問題描述
在我現在正在進行的項目中,我經常看到 @Spy
和 @InjectMocks
在字段上一起使用.我從未在任何教程或其他資源中看到過這種方式.我用谷歌搜索了這個特定的組合,但沒有在 GitHub 上找到除此線程之外的任何其他內容:https://github.com/mockito/mockito/issues/169
這讓我覺得我們以一種奇怪的方式使用它.
注意:我認為同時使用兩個注解的原因有時是有意義的,因為如果你只使用 @InjectMocks
Mockito 嘗試使用無參數構造函數實例化該類.如果您沒有無參數構造函數并添加 @Spy
您可以使用該對象而無需空構造函數.
另一個重要的用途是你只能存根方法如果您只使用兩個注釋.
將@Spy 和@InjectMocks 一起使用是不常見的,而且可以說是不合適的.
@InjectMocks 作為被測系統的一種依賴注入:如果您有一個定義了正確類型的 @Mock 或 @Spy 的測試,Mockito 將初始化您的任何字段@InjectMocks 具有這些字段的實例.如果您沒有為依賴注入構建被測系統(或者如果您使用執行字段注入的 DI 框架)并且您想用模擬替換這些依賴關系,這可能會很方便.它可能非常脆弱——不匹配的字段將被靜默忽略,并且如果沒有在初始化程序中設置,將保持 null
——但對于您的測試系統來說仍然是一個不錯的注釋.p>
@Spy 和@Mock 一樣,旨在設置測試替身;當你有一個想要存根或驗證的合作者時,你應該使用它.請注意,@Spy 和 @Mock 始終用于依賴項,而不是用于您的測試系統.
理想情況下,您不應該有任何類在同一個測試中同時滿足這兩個角色,否則您可能會發現自己編寫的測試會煞費苦心地測試您存根的行為而不是實際的生產行為.無論如何,要準確判斷測試涵蓋的內容與您存根的行為將更加困難.
當然,如果您嘗試使用 Mockito 單獨測試單個方法,并且您希望在測試另一種方法時對一個方法的調用存根,這可能不適用.但是,這也可能表明您的班級違反了單一職責原則,并且您應該將班級分解為多個相互合作的獨立班級.然后,在您的測試中,您可以允許實例僅具有一個角色,而永遠不需要同時使用兩個注釋.
In the project I'm working on right now,
I often see the @Spy
and @InjectMocks
used together on a field.
I have never seen it this way in any tutorials or other resources.
I googled about this specific combination but didn't
find anything else other than this thread on GitHub:
https://github.com/mockito/mockito/issues/169
Which makes me think we are using it in a weird way.
Note: The reason why I think using both annotations together
makes sense sometimes is because if you only use @InjectMocks
Mockito tries to instantiate the class with a no-args constructor.
If you don't have a no-args contructor and add @Spy
you can use the object without needing an empty constructor.
Edit: Another important use is that you can only stub methods if you just use both annotations.
It is uncommon, and arguably inappropriate, to use @Spy and @InjectMocks together.
@InjectMocks works as a sort of dependency injection for the system under test: If you have a test that defines a @Mock or @Spy of the right type, Mockito will initialize any fields in your @InjectMocks instance with those fields. This might be handy if you haven't otherwise structured your system-under-test for dependency injection (or if you use a DI framework that does field injection) and you want to replace those dependencies with mocks. It can be pretty fragile—unmatched fields will be silently ignored and will remain null
if not set in an initializer—but remains a decent annotation for your system under test.
@Spy, like @Mock, is designed to set up test doubles; you should use it when you have a collaborator that you want to stub or verify. Note there that @Spy and @Mock are always meant for dependencies, and not for your system under test.
Ideally, you should not have any class that fulfills both roles in the same test, or else you may find yourself writing a test that painstakingly tests behavior that you've stubbed rather than actual production behavior. In any case it will be more difficult to tell exactly what the test covers versus the behavior you've stubbed.
Of course, this may not apply if you're trying to use Mockito to test a single method in isolation, and you want to stub calls to one method while testing the other. However, this might also be an indication that your class is violating the Single Responsibility Principle, and that you should break down the class into multiple independent classes that work together. Then, in your test, you can allow instances to have exactly one role and never need both annotations at once.
這篇關于是否不鼓勵在同一領域使用 @Spy 和 @InjectMocks?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!