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

Mockito 與 JMockit 之間的比較 - 為什么 Mockito 的投票

Comparison between Mockito vs JMockit - why is Mockito voted better than JMockit?(Mockito 與 JMockit 之間的比較 - 為什么 Mockito 的投票比 JMockit 好?)
本文介紹了Mockito 與 JMockit 之間的比較 - 為什么 Mockito 的投票比 JMockit 好?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在研究為我的項目使用哪個模擬框架,并將其范圍縮小到 JMockit 和 Mockito.

I'm investigating which mocking framework to use for my project and have narrowed it down to JMockit and Mockito.

我注意到 Mockito 被選為the最好的 Java 模擬框架"在 Stackoverflow 上.
在比較 JMockit 的模擬工具的功能時比較矩陣",JMockit 似乎有多種不同的特征.

I notice that Mockito was voted "the best mock framework for Java" on Stackoverflow.
In comparing features on JMockit's "Mocking Tool Comparision Matrix" it appears that JMockit has multiple different features.

對于 Mockito 可以做什么而 JMockit 無法實現的事情,是否有人有任何具體信息(不是意見),反之亦然?

Does anyone have any specific information (not opinions) on what Mockito can do which can't be achieved with JMockit and vice versa?

推薦答案

2019年9月更新:唯一的Spring Boot 支持的模擬框架(默認) 是 Mockito.如果你使用 Spring,答案就很明顯了.

Update Sep 2019: The only mocking framework supported (by default) by Spring Boot is Mockito. If you use Spring, the answer is quite obvious.

我想說的是JMockitPowerMock之間的競爭,然后是Mockito.

I'd say the competition is between JMockit and PowerMock, then Mockito.

我會留下普通"的 jMock 和 EasyMock,因為它們只使用代理和CGLIB,并且不要像新框架那樣使用 Java 5 工具.

I'd leave "plain" jMock and EasyMock because they use only proxy & CGLIB and do not use Java 5 instrumentation like the newer frameworks.

jMock 也有超過 4 年沒有穩定的版本.jMock 2.6.0 從 RC1 升級到 RC2 需要 2 年,然后再需要 2 年才能真正發布.

jMock also didn't have a stable release for over 4 years. jMock 2.6.0 required 2 years to go from RC1 to RC2, and then another 2 years before it actually got released.

關于代理和CGLIB 與儀表:

Regarding Proxy & CGLIB vs instrumentation:

(EasyMock 和 jMock)基于 java.lang.reflect.Proxy,這需要一個接口實施的.此外,他們支持創建模擬對象對于通過 CGLIB 子類的類一代.正因為如此,說類不能是最終的,只有可覆蓋的實例方法可以是嘲笑.然而,最重要的是,使用這些工具時被測代碼的依賴項(即是,其他類的對象哪個給定的類正在測試取決于)必須由控制測試,以便模擬實例可以傳遞給那些客戶依賴關系.因此,依賴不能簡單地用客戶端類中的新運算符我們要編寫單元測試.

(EasyMock and jMock) are based on java.lang.reflect.Proxy, which requires an interface to be implemented. Additionally, they support the creation of mock objects for classes through CGLIB subclass generation. Because of that, said classes cannot be final and only overridable instance methods can be mocked. Most importantly, however, when using these tools the dependencies of code under test (that is, the objects of other classes on which a given class under test depends) must be controlled by the tests, so that mock instances can be passed to the clients of those dependencies. Therefore, dependencies can't simply be instantiated with the new operator in a client class for which we want to write unit tests.

最終,技術限制傳統的模擬工具強加以下設計限制生產代碼:

Ultimately, the technical limitations of conventional mocking tools impose the following design restrictions on production code:

  1. 在測試中可能需要模擬的每個類都必須實現一個單獨的接口或不是最終的.
  2. 每個要測試的類的依賴必須要么獲取通過可配置的實例創建方法(工廠或服務定位器),或暴露于依賴注射.否則,單元測試不會能夠通過模擬實現對下屬單位的依賴測試.
  3. 由于只能模擬實例方法,因此要對類進行單元測試不能調用任何靜態方法它們的依賴關系,也不實例化他們使用任何構造函數.

以上內容復制自 http://jmockit.org/about.html .此外,它還通過多種方式在自身 (JMockit)、PowerMock 和 Mockito 之間進行比較:

The above is copied from http://jmockit.org/about.html . Further, it compares between itself (JMockit), PowerMock, and Mockito in several ways:

現在有其他模擬工具Java里面也克服了傳統方法的局限,它們之間有 PowerMock、jEasyTest 和模擬注入.最接近的那個JMockit 的功能集是PowerMock,所以我將簡要評估一下它在這里(此外,其他兩個是更有限,似乎不是不再積極開發).

There are now other mocking tools for Java which also overcome the limitations of the conventional ones, between them PowerMock, jEasyTest, and MockInject. The one that comes closest to the feature set of JMockit is PowerMock, so I will briefly evaluate it here (besides, the other two are more limited and don't seem to be actively developed anymore).

  • 首先,PowerMock沒有提供完整的模擬API,而是作為擴展另一種工具,目前可以EasyMock 或 Mockito.這顯然是現有用戶的優勢那些工具.
  • 另一方面,JMockit 提供了全新的 API,盡管它的主要API(期望)是相似的EasyMock 和 jMock.雖然這創造了更長的學習曲線,它還允許 JMockit 提供一個更簡單、更一致、更容易使用 API.
  • 與 JMockit Expectations API 相比,PowerMock API 是更低級",迫使用戶找出并指定哪些類需要為測試做好準備(與@PrepareForTest({ClassA.class,...}) 注釋)并要求特定的 API 調用來處理各種語言結構生產中可能存在的代碼:靜態方法(mockStatic(ClassA.class)),構造函數(抑制(構造函數(ClassXyz.class))),構造函數調用(expectNew(AClass.class)), 部分模擬(createPartialMock(ClassX.class,"methodToMock")) 等.
  • 有了JMockit Expectations,各種方法和構造函數以純粹的聲明方式嘲笑,通過指定的部分模擬@Mocked 中的正則表達式注釋或簡單地取消模擬"沒有記錄的成員期望;也就是開發商簡單地聲明一些共享的模擬字段"用于測試類,或一些本地模擬字段"和/或模擬參數"用于個別測試方法(在最后一種情況下@Mocked 注釋通常不會需要).
  • JMockit 中的一些可用功能,例如對模擬的支持等于和 hashCode,被覆蓋方法和其他方法目前還沒有PowerMock 支持.此外,還有不等同于 JMockit 的能力捕獲實例并模擬指定基礎的實現類型作為測試執行,沒有測試代碼本身有任何實際執行的知識類.
  • PowerMock 使用自定義類加載器(通常每個測試類一個)為了生成修改版本的嘲笑類.如此大量的使用的自定義類加載器可能導致與第三方庫沖突,因此有時需要使用@PowerMockIgnore("package.to.be.ignored")測試類的注釋.
  • JMockit 使用的機制(通過Java 代理")更簡單、更安全,雖然它確實需要通過-javaagent"參數到 JVM 時在 JDK 1.5 上開發;在 JDK 1.6+(它總是可以用于開發,即使部署在舊版本)沒有這樣的要求,因為 JMockit 可以透明地加載 Java 代理使用 Attach API 來滿足需求.

另一個最近的模擬工具是莫基托.雖然沒有嘗試克服老年人的局限工具(jMock,EasyMock),它確實引入一種新的行為方式用模擬測試.JMockit 也支持這種另類風格,通過 Verifications API.

Another recent mocking tool is Mockito. Although it does not attempt to overcome the limitations of older tools (jMock, EasyMock), it does introduce a new style of behavior testing with mocks. JMockit also supports this alternative style, through the Verifications API.

  • Mockito 依賴對其 API 的顯式調用來分離代碼記錄之間 (when(...)) 和驗證(驗證(...))階段.這意味著對模擬的任何調用測試代碼中的對象也需要對模擬 API 的調用.此外,這通常會導致重復 when(...) 和驗證(模擬)...調用.
  • 使用 JMockit,不存在類似的調用.當然,我們有新的NonStrictExpectations() 和新的Verifications() 構造函數調用,但是它們每次測試只發生一次(通常),并且完全與調用分開模擬方法和構造函數.
  • Mockito API 在用于對模擬方法的調用.在里面記錄階段,我們有這樣的電話何時(mock.mockedMethod(args))...而在驗證階段這個相同的調用將被寫為驗證(模擬).mockedMethod(參數).請注意,在第一種情況下調用 mockedMethod直接在模擬對象上,而在第二種情況是在verify(mock) 返回的對象.
  • JMockit 沒有這樣的不一致,因為調用總是制作模擬方法直接在模擬實例上他們自己.(只有一個例外:匹配相同的調用模擬實例,一個 onInstance(mock)調用被使用,導致代碼像onInstance(mock).mockedMethod(args);大多數測試不需要使用它,不過.)
  • 就像其他依賴方法的模擬工具一樣鏈接/包裝,Mockito 也運行存根時語法不一致無效的方法.例如,你寫when(mockedList.get(1)).thenThrow(new運行時異常());對于非空方法和 doThrow(newRuntimeException()).when(mockedList).clear();對于一個無效的.使用 JMockit,它是總是相同的語法:mockedList.clear();結果=新運行時異常();.
  • 在使用 Mockito 間諜時還存在另一個不一致之處:模擬"允許真正的方法是在間諜實例上執行.為了例如,如果 spy 指的是一個空的列出,然后而不是寫when(spy.get(0)).thenReturn("foo") 你需要寫doReturn("foo").when(spy).get(0).和JMockit,動態模擬功能提供類似的功能間諜,但沒有這個問題,因為真正的方法只在重播階段.
  • 在 EasyMock 和 jMock(Java 的第一個模擬 API)中,重點是完全基于預期的記錄模擬方法的調用,對于(默認情況下)不模擬的對象允許意外調用.那些API 還提供記錄允許對模擬對象的調用確實允許意外調用,但這被視為二等特征.此外,有了這些工具沒有辦法明確在被測代碼被執行.所有這樣的隱式執行驗證并自動進行.
  • 在 Mockito(以及 Unitils Mock)中,相反的觀點是采取.對模擬對象的所有調用在測試期間可能發生的情況,無論是否記錄,都允許,沒想到.驗證是在代碼之后顯式執行在測試中被行使,從不自動.
  • 這兩種方法都過于極端,因此都不是最優的.JMockit 期望與驗證是唯一允許開發人員可以無縫選擇嚴格的最佳組合(預期默認情況下)和非嚴格(允許默認)為每個模擬調用測試.
  • 更清楚地說,Mockito API 有以下缺點.如果你需要驗證對 a 的調用非空模擬方法發生在測試,但測試需要一個從該方法返回值是不同于默認的返回類型,然后是 Mockito 測試將有重復的代碼:awhen(mock.someMethod()).thenReturn(xyz)在記錄階段調用,以及verify(mock).someMethod() 在驗證階段.使用 JMockit,一個嚴格的期待總能被記錄,不必明確已驗證.或者,調用計數約束(次 = 1)可以是指定用于任何記錄的非嚴格期望(與 Mockito 這樣的約束只能在一個驗證(模擬,約束)調用).
  • Mockito 的語法很差,無法按順序進行驗證驗證(即,檢查所有對模擬對象的調用都是明確驗證).在第一情況下,需要一個額外的對象創建,并調用驗證它: InOrder inOrder = inOrder(mock1,模擬2,...).在第二種情況下,調用像 verifyNoMoreInteractions(mock) 或verifyZeroInteractions(mock1, mock2)需要制作.
  • 使用 JMockit,您只需編寫 new VerificationsInOrder() 或 newFullVerifications() 而不是 new驗證()(或新FullVerificationsInOrder() 結合兩個要求).無需指定涉及哪些模擬對象.不額外的模擬 API 調用.而作為一個獎金,通過調用unverifiedInvocations() 內有序的驗證塊,你可以執行與訂單相關的驗證這在 Mockito 中根本不可能.

最后,JMockit 測試工具包范圍更廣并且更有野心目標 比其他模擬工具包,在為了提供一個完整的和復雜的開發人員測試解決方案.一個很好的模擬 API,甚至沒有人為的限制,不是足以生產性地創造測試.與 IDE 無關,易于使用,和集成良好的代碼覆蓋率工具也是必不可少的,這就是JMockit Coverage 旨在提供.開發人員測試的另一部分將變得更有用的工具集隨著測試套件規模的擴大,增量重新運行測試的能力在對生產進行局部更改后代碼;這也包含在覆蓋工具.

Finally, the JMockit Testing Toolkit has a wider scope and more ambitious goals than other mocking toolkits, in order to provide a complete and sophisticated developer testing solution. A good API for mocking, even without artificial limitations, is not enough for productive creation of tests. An IDE-agnostic, easy to use, and well integrated Code Coverage tool is also essential, and that's what JMockit Coverage aims to provide. Another piece of the developer testing toolset which will become more useful as the test suite grows in size is the ability to incrementally rerun tests after a localized change to production code; this is also included in the Coverage tool.

(當然,來源可能有偏見,但好吧......)

(granted, the source may be biased, but well...)

我會說使用 JMockit.當您無法控制要測試的類(或由于兼容性等原因而無法破壞它)時,它最容易使用、靈活,并且適用于幾乎所有情況,甚至是困難的情況和場景.

I'd say go with JMockit. It's the easiest to use, flexible, and works for pretty much all cases even difficult ones and scenarios when you can't control the class to be tested (or you can't break it due to compatibility reasons etc.).

我對 JMockit 的體驗非常積極.

My experiences with JMockit have been very positive.

這篇關于Mockito 與 JMockit 之間的比較 - 為什么 Mockito 的投票比 JMockit 好?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 欧美在线观看免费观看视频 | 国产精品自产拍 | 精品欧美一区二区精品久久久 | av黄色在线 | 中文字幕在线观看一区 | 精品视频一区二区在线观看 | 亚洲国产免费 | 国产一级一级国产 | 亚洲精品二区 | 在线播放国产视频 | 久久久久久九九九九 | 成年人黄色一级毛片 | 国产高清精品一区二区三区 | 欧美激情一区二区三级高清视频 | 免费在线观看一区二区 | 91超碰在线观看 | 日韩一区二区三区在线观看 | 日本精品免费在线观看 | 欧美性生活网 | av中文字幕在线播放 | 午夜在线视频 | 精品在线看 | 国产视频二区 | 真人女人一级毛片免费播放 | 二区三区在线观看 | 成人在线观看免费 | 欧美日韩在线看 | 在线国产一区二区三区 | 国产精品五月天 | www.日韩在线 | 欧美高清性xxxxhd | 韩日一区二区 | www日本在线观看 | 国产精品18hdxxxⅹ在线 | 久久久久久亚洲国产精品 | 爱爱免费视频 | 影音av | 日韩1区 | 麻豆av片| www.日本三级 | 欧美国产精品 |