久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

是否不鼓勵(lì)在同一領(lǐng)域使用 @Spy 和 @InjectMocks?

Is it discouraged to use @Spy and @InjectMocks on the same field?(是否不鼓勵(lì)在同一領(lǐng)域使用 @Spy 和 @InjectMocks?)
本文介紹了是否不鼓勵(lì)在同一領(lǐng)域使用 @Spy 和 @InjectMocks?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在我現(xiàn)在正在進(jìn)行的項(xiàng)目中,我經(jīng)常看到 @Spy@InjectMocks 在字段上一起使用.我從未在任何教程或其他資源中看到過這種方式.我用谷歌搜索了這個(gè)特定的組合,但沒有在 GitHub 上找到除此線程之外的任何其他內(nèi)容:https://github.com/mockito/mockito/issues/169

這讓我覺得我們以一種奇怪的方式使用它.

注意:我認(rèn)為同時(shí)使用兩個(gè)注解的原因有時(shí)是有意義的,因?yàn)槿绻阒皇褂?@InjectMocksMockito 嘗試使用無參數(shù)構(gòu)造函數(shù)實(shí)例化該類.如果您沒有無參數(shù)構(gòu)造函數(shù)并添加 @Spy 您可以使用該對(duì)象而無需空構(gòu)造函數(shù).

另一個(gè)重要的用途是你只能存根方法如果您只使用兩個(gè)注釋.

解決方案

將@Spy 和@InjectMocks 一起使用是不常見的,而且可以說是不合適的.

@InjectMocks 作為被測系統(tǒng)的一種依賴注入:如果您有一個(gè)定義了正確類型的 @Mock 或 @Spy 的測試,Mockito 將初始化您的任何字段@InjectMocks 具有這些字段的實(shí)例.如果您沒有為依賴注入構(gòu)建被測系統(tǒng)(或者如果您使用執(zhí)行字段注入的 DI 框架)并且您想用模擬替換這些依賴關(guān)系,這可能會(huì)很方便.它可能非常脆弱——不匹配的字段將被靜默忽略,并且如果沒有在初始化程序中設(shè)置,將保持 null——但對(duì)于您的測試系統(tǒng)來說仍然是一個(gè)不錯(cuò)的注釋.p>

@Spy 和@Mock 一樣,旨在設(shè)置測試替身;當(dāng)你有一個(gè)想要存根或驗(yàn)證的合作者時(shí),你應(yīng)該使用它.請(qǐng)注意,@Spy 和 @Mock 始終用于依賴項(xiàng),而不是用于您的測試系統(tǒng).

理想情況下,您不應(yīng)該有任何類在同一個(gè)測試中同時(shí)滿足這兩個(gè)角色,否則您可能會(huì)發(fā)現(xiàn)自己編寫的測試會(huì)煞費(fèi)苦心地測試您存根的行為而不是實(shí)際的生產(chǎn)行為.無論如何,要準(zhǔn)確判斷測試涵蓋的內(nèi)容與您存根的行為將更加困難.

當(dāng)然,如果您嘗試使用 Mockito 單獨(dú)測試單個(gè)方法,并且您希望在測試另一種方法時(shí)對(duì)一個(gè)方法的調(diào)用存根,這可能不適用.但是,這也可能表明您的班級(jí)違反了單一職責(zé)原則,并且您應(yīng)該將班級(jí)分解為多個(gè)相互合作的獨(dú)立班級(jí).然后,在您的測試中,您可以允許實(shí)例僅具有一個(gè)角色,而永遠(yuǎn)不需要同時(shí)使用兩個(gè)注釋.

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.

這篇關(guān)于是否不鼓勵(lì)在同一領(lǐng)域使用 @Spy 和 @InjectMocks?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級(jí)參考(在超級(jí)類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對(duì)象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗(yàn)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 国产成人99久久亚洲综合精品 | 成人精品一区二区三区 | 久久精品黄色 | 亚洲精品一区二区二区 | 国内在线视频 | 99re超碰| 欧美性大战xxxxx久久久 | 久久久久久国产精品免费免费狐狸 | 成人精品毛片 | 日韩欧美二区 | 91精品国产综合久久香蕉麻豆 | 欧美成人精品在线 | 国产一级片 | 日本h片在线观看 | 精品视频久久久久久 | 亚洲天堂av网 | 欧洲成人免费视频 | 国产精成人 | 一区二区影院 | 亚洲国产高清高潮精品美女 | 狠狠做深爱婷婷综合一区 | 日韩免费高清视频 | 国产精品一区二区视频 | 久久av综合 | 自拍中文字幕 | 天堂一区二区三区 | 麻豆久久久 | 久久综合成人精品亚洲另类欧美 | 欧美亚洲激情 | 亚洲一区在线日韩在线深爱 | 日本成人免费网站 | 日本啊v在线 | 成年人在线视频 | 秋霞影院一区二区 | 日韩精品一区二区三区在线观看 | 一级片在线视频 | h漫在线观看 | 国产四区| 特级黄一级播放 | re久久| 一区二区三区不卡视频 |