mockito_Mockito和Hamcrest的试驾制造商
mockito
過去,很多人問我是否測試吸氣劑和吸氣劑(屬性,屬性等)。 他們還問我是否測試我的建筑商。 在我看來,答案取決于情況。當使用遺留代碼時,我不會費心去測試數據結構,這意味著對象只帶有getter和setter,映射,列表等。原因之一是我從不模擬它們。 在測試使用它們的類時,我照原樣使用它們。 對于構建器,當它們僅由測試類使用時,我也不會對其進行單元測試,因為它們在許多其他測試中均被用作“幫助者”。 如果它們有錯誤,則測試將失敗。 總而言之,如果這些數據結構和構建器已經存在,那么我不會為它們進行改裝測試。
但是,現在讓我們談談進行TDD并假設您需要一個帶有getter和setter的新對象。 在這種情況下,是的,我將為吸氣劑和吸氣劑編寫測試,因為我需要先編寫測試來證明它們的存在。
為了擁有豐富的領域模型,我通常傾向于將業務邏輯與數據相關聯,并擁有更豐富的領域模型。 讓我們看下面的例子。
在現實生活中,我會一次編寫測試,使它們通過并重構。 在這篇文章中,為清晰起見,我僅向您提供完整的課程。 首先讓我們編寫測試:
package org.craftedsw.testingbuilders;import static org.hamcrest.Matchers.is;import static org.junit.Assert.assertThat;import static org.mockito.Matchers.anyString;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;@RunWith(MockitoJUnitRunner.class)public class TradeTest {private static final String INBOUND_XML_MESSAGE = '<message >';private static final boolean REPORTABILITY_RESULT = true;private Trade trade;@Mock private ReportabilityDecision reportabilityDecision;@Beforepublic void initialise() {trade = new Trade();when(reportabilityDecision.isReportable(anyString())).thenReturn(REPORTABILITY_RESULT);}@Test public voidshould_contain_the_inbound_xml_message() {trade.setInboundMessage(INBOUND_XML_MESSAGE);assertThat(trade.getInboundMessage(), is(INBOUND_XML_MESSAGE));}@Test public voidshould_tell_if_it_is_reportable() {trade.setInboundMessage(INBOUND_XML_MESSAGE);trade.setReportabilityDecision(reportabilityDecision);boolean reportable = trade.isReportable();verify(reportabilityDecision).isReportable(INBOUND_XML_MESSAGE);assertThat(reportable, is(REPORTABILITY_RESULT));}}現在執行:
package org.craftedsw.testingbuilders;public class Trade {private String inboundMessage;private ReportabilityDecision reportabilityDecision;public String getInboundMessage() {return this.inboundMessage;}public void setInboundMessage(String inboundXmlMessage) {this.inboundMessage = inboundXmlMessage;}public boolean isReportable() {return reportabilityDecision.isReportable(inboundMessage);}public void setReportabilityDecision(ReportabilityDecision reportabilityDecision) {this.reportabilityDecision = reportabilityDecision;}}這種情況很有趣,因為Trade對象具有一個名為inboundMessage的屬性,具有相應的getter和setter,并且在isReportable業務方法中還使用了一個協作者(reportabilityDecision,通過setter注入)。
我多次見過的“測試” setReportabilityDecision方法的常見方法是引入getReportabilityDecision方法,該方法返回reportabilityDecision(協作者)對象。
這絕對是錯誤的方法。 我們的目標應該是測試協作器的使用方式,即是否使用正確的參數調用協作器,以及是否使用返回的任何東西(如果返回任何東西)。 在這種情況下引入吸氣劑是沒有意義的,因為它不能保證在通過設置器注入了協作者之后,對象將按照我們的預期與協作者進行交互。
順便說一句,當我們編寫有關將如何使用協作者的測試時,定義它們的接口是在將TDD用作設計工具而不僅僅是將其用作測試工具時。 我將在以后的博客文章中進行介紹。
好的,現在假設可以以不同的方式(即具有不同的可報告性決策)創建此貿易對象。 我們還希望使代碼更具可讀性,并決定為Trade對象編寫一個生成器。 在這種情況下,我們還假設我們希望生成器也用于生產和測試代碼中。 在這種情況下,我們要測試驅動器。
這是我通常在開發人員測試驅動構建器實現時發現的一個示例。
package org.craftedsw.testingbuilders;import static org.craftedsw.testingbuilders.TradeBuilder.aTrade;import static org.hamcrest.Matchers.is;import static org.junit.Assert.assertThat;import static org.mockito.Mockito.verify;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.Mock;import org.mockito.runners.MockitoJUnitRunner;@RunWith(MockitoJUnitRunner.class)public class TradeBuilderTest {private static final String TRADE_XML_MESSAGE = '<message >';@Mockprivate ReportabilityDecision reportabilityDecision;@Test public voidshould_create_a_trade_with_inbound_message() {Trade trade = aTrade().withInboundMessage(TRADE_XML_MESSAGE).build();assertThat(trade.getInboundMessage(), is(TRADE_XML_MESSAGE));}@Test public voidshould_create_a_trade_with_a_reportability_decision() {Trade trade = aTrade().withInboundMessage(TRADE_XML_MESSAGE).withReportabilityDecision(reportabilityDecision).build();trade.isReportable();verify(reportabilityDecision).isReportable(TRADE_XML_MESSAGE);}}現在讓我們看看這些測試。 好消息是,測試以開發人員希望閱讀的方式編寫。 這也意味著他們正在“設計” TradeBuilder公共接口(公共方法)。 壞消息是他們如何測試它。
如果仔細看,構建器的測試與TradeTest類中的測試幾乎相同。
您可能會說沒問題,因為構建器正在創建對象,并且測試應該相似。 唯一的不同是,在TradeTest中我們手動實例化對象,在TradeBuilderTest中我們使用構建器實例化對象,但是斷言應該相同,對嗎?
對我來說,首先我們要重復。 其次,TradeBuilderTest沒有顯示出它的真實意圖。 經過多次重構和探索不同的想法之后,在與團隊中的一個人進行配對編程時,我們想到了這種方法:
因此,現在,TradeBuilderTest表達了TradeBuilder的期望,即調用build方法時的副作用。 我們希望它創建交易并設置其屬性。 TradeTest沒有重復項。 它留給TradeTest來保證Trade對象的正確行為。
為了完善起見,這是最后的TradeBuider類:
Mockito和Hamcrest的結合非常強大,使我們能夠編寫更好,更易讀的測試。
參考:來自Crafts Software博客的JCG合作伙伴 Sandro Mancuso的Mockito和Hamcrest的測試驅動構建器 。
翻譯自: https://www.javacodegeeks.com/2012/06/test-driving-builders-with-mockito-and.html
mockito
總結
以上是生活随笔為你收集整理的mockito_Mockito和Hamcrest的试驾制造商的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux镜像文件(linux 文件镜像
- 下一篇: 如何在单个API中支持Java 6、8、