問題描述
我為 3 個(gè)目的編寫 jUnit 測試用例:
I write jUnit test cases for 3 purposes:
- 為了確保我的代碼在所有(或大部分)輸入組合/值下滿足所有必需的功能.
- 為了確保我可以更改實(shí)現(xiàn),并依靠 JUnit 測試用例告訴我我的所有功能仍然得到滿足.
- 作為我的代碼處理的所有用例的文檔,并充當(dāng)重構(gòu)規(guī)范 - 如果代碼需要重寫.(重構(gòu)代碼,如果我的 jUnit 測試失敗 - 你可能錯(cuò)過了一些用例).
我不明白為什么或何時(shí)應(yīng)該使用 Mockito.verify()
.當(dāng)我看到 verify()
被調(diào)用時(shí),它告訴我我的 jUnit 正在意識(shí)到該實(shí)現(xiàn).(因此更改我的實(shí)現(xiàn)會(huì)破壞我的 jUnit,即使我的功能不受影響).
I do not understand why or when Mockito.verify()
should be used. When I see verify()
being called, it is telling me that my jUnit is becoming aware of the implementation. (Thus changing my implementation would break my jUnits, even though my functionality was unaffected).
我正在尋找:
正確使用
Mockito.verify()
的指南應(yīng)該是什么?
jUnits 了解或緊密耦合到被測類的實(shí)現(xiàn)是否從根本上正確?
Is it fundamentally correct for jUnits to be aware of, or tightly coupled to, the implementation of the class under test?
推薦答案
如果類 A 的契約包括它調(diào)用 C 類型對象的方法 B 的事實(shí),那么您應(yīng)該通過制作 C 類型的模擬來測試這一點(diǎn),并驗(yàn)證方法 B 是否已被調(diào)用.
If the contract of class A includes the fact that it calls method B of an object of type C, then you should test this by making a mock of type C, and verifying that method B has been called.
這意味著類 A 的契約有足夠的細(xì)節(jié)來討論類型 C(可能是接口或類).所以是的,我們談?wù)摰囊?guī)范級別不僅僅是系統(tǒng)要求",而是在某種程度上描述了實(shí)現(xiàn).
This implies that the contract of class A has sufficient detail that it talks about type C (which might be an interface or a class). So yes, we're talking about a level of specification that goes beyond just "system requirements", and goes some way to describing implementation.
這對于單元測試是正常的.當(dāng)您進(jìn)行單元測試時(shí),您希望確保每個(gè)單元都在做正確的事情",這通常包括它與其他單元的交互.這里的單元"可能是指類或應(yīng)用程序的更大子集.
This is normal for unit tests. When you are unit testing, you want to ensure that each unit is doing the "right thing", and that will usually include its interactions with other units. "Units" here might mean classes, or larger subsets of your application.
更新:
我覺得這不僅適用于驗(yàn)證,也適用于存根.一旦您存根協(xié)作者類的方法,您的單元測試就在某種意義上變得依賴于實(shí)現(xiàn).這有點(diǎn)像單元測試的性質(zhì).由于 Mockito 與驗(yàn)證一樣重要,因此您使用 Mockito 的事實(shí)意味著您將遇到這種依賴關(guān)系.
I feel that this doesn't apply just to verification, but to stubbing as well. As soon as you stub a method of a collaborator class, your unit test has become, in some sense, dependent on implementation. It's kind of in the nature of unit tests to be so. Since Mockito is as much about stubbing as it is about verification, the fact that you're using Mockito at all implies that you're going to run across this kind of dependency.
根據(jù)我的經(jīng)驗(yàn),如果我改變一個(gè)類的實(shí)現(xiàn),我經(jīng)常不得不改變它的單元測試的實(shí)現(xiàn)來匹配.不過,通常情況下,我不必更改類是的單元測試清單;當(dāng)然,除非更改的原因是存在我之前未能測試的條件.
In my experience, if I change the implementation of a class, I often have to change the implementation of its unit tests to match. Typically, though, I won't have to change the inventory of what unit tests there are for the class; unless of course, the reason for the change was the existence of a condition that I failed to test earlier.
這就是單元測試的意義所在.不受這種對協(xié)作者類使用方式的依賴影響的測試實(shí)際上是子系統(tǒng)測試或集成測試.當(dāng)然,這些也經(jīng)常使用 JUnit 編寫,并且經(jīng)常涉及到 mocking 的使用.在我看來,JUnit"是一個(gè)糟糕的名字,因?yàn)樗梢宰屛覀冞M(jìn)行所有不同類型的測試.
So this is what unit tests are about. A test that doesn't suffer from this kind of dependency on the way collaborator classes are used is really a sub-system test or an integration test. Of course, these are frequently written with JUnit too, and frequently involve the use of mocking. In my opinion, "JUnit" is a terrible name, for a product that lets us produce all different types of test.
這篇關(guān)于何時(shí)使用 Mockito.verify()?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!