日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

idea内置junit5_JUnit的内置Hamcrest Core Matcher支持

發布時間:2023/12/3 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 idea内置junit5_JUnit的内置Hamcrest Core Matcher支持 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

idea內置junit5

在通過JUnit和Hamcrest改善對assertEquals的文章中,我簡要討論了Hamcrest “ 核心 ”匹配器與JUnit的現代版本“結合”在一起的情況。 在那篇文章中,我特別關注了JUnit的assertThat(T,Matcher)靜態方法與Hamcrest核心is()匹配器的結合使用,該方法自動包含在更高版本的JUnit中。 在本文中,我將介紹與最新版本的JUnit捆綁在一起的其他Hamcrest“核心”匹配器。

JUnit包括開箱即用的Hamcrest “核心”匹配器的兩個優點是,無需專門下載Hamcrest,也無需在單元測試類路徑中明確包含它。 在查看更多方便的Hamcrest“核心”匹配器之前,重要的是要在此指出,我有意重復提及“核心” Hamcrest匹配器,因為JUnit的最新版本僅提供“核心”( 并非全部 )Hamcrest匹配器自動。 仍然需要單獨下載核心匹配器之外的任何Hamcrest匹配器,并在單元測試類路徑上明確指定。 了解什么是Hamcrest“核心”(以及在最新版本的JUnit中默認情況下可用的匹配器)的一種方法是查看該程序包的基于Javadoc的API文檔 :

從JUnit提供的org.hamcrest.core軟件包的文檔中,我們可以找到以下匹配器(及其描述):

類 Javadoc類說明 覆蓋這里? AllOf <T> AnyOf <T> DescribedAs <T> 是<T> IsAnything <T> IsEqual <T> IsInstanceOf IsNot <T> IsNull <T> IsSame <T>
計算兩個匹配器的邏輯合。
計算兩個匹配器的邏輯和。
為另一個匹配器提供自定義描述。
裝飾另一個Matcher,保留其行為,但允許測試更具表現力。 再次
始終返回true的匹配器。 沒有
根據Object.equals(java.lang.Object)invokedMethod測試,該值是否等于另一個值?
測試值是否為類的實例。
計算匹配器的邏輯取反。
值是否為空?
該值與另一個值是同一對象嗎?

在我先前的演示Hamcrest is()匹配器與JUnit的assertThat()結合使用的文章中,我使用了IntegerArithmetic實現作為測試工具。 我將在這里再次使用它來演示其他一些Hamcrest核心匹配器。 為方便起見,該類復制如下。

IntegerArithmetic.java

package dustin.examples;/*** Simple class supporting integer arithmetic.* * @author Dustin*/ public class IntegerArithmetic {/*** Provide the product of the provided integers.* * @param firstInteger First integer to be multiplied.* @param secondInteger Second integer to be multiplied.* @param integers Integers to be multiplied together for a product.* @return Product of the provided integers.* @throws ArithmeticException Thrown in my product is too small or too large* to be properly represented by a Java integer.*/public int multiplyIntegers(final int firstInteger, final int secondInteger, final int ... integers){int returnInt = firstInteger * secondInteger;for (final int integer : integers){returnInt *= integer;}return returnInt;} }

在“ 使用JUnit和Hamcrest改進assertEquals”中 ,我主要依靠is()來比較預期結果和實際結果,以測試整數乘法。 另一個選擇是使用equalTo匹配器,如下面的代碼清單所示。

使用Hamcrest equalTo()

/*** Test of multiplyIntegers method, of class IntegerArithmetic, using core* Hamcrest matcher equalTo.*/@Testpublic void testWithJUnitHamcrestEqualTo(){final int[] integers = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15;final int result = this.instance.multiplyIntegers(2, 3, integers);assertThat(result, equalTo(expectedResult));}

盡管不是必需的,但有些開發人員喜歡將is和equalTo一起使用,因為它們對他們來說更流利。 這就是is的存在的原因:更流暢地使用其他匹配器。 我經常equalTo()使用is() (暗示equalTo() ),如通過JUnit和Hamcrest改進對assertEquals所討論的。 下一個示例演示了將is()匹配器與equalTo匹配器結合使用。

將Hamcrest equalTo()與is()結合使用

/*** Test of multiplyIntegers method, of class IntegerArithmetic, using core* Hamcrest matcher equalTo with "is" Matcher..*/@Testpublic void testWithJUnitHamcrestEqualToAndIsMatchers(){final int[] integers = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15;final int result = this.instance.multiplyIntegers(2, 3, integers);assertThat(result, is(equalTo(expectedResult)));}

equalTo Hamcrest匹配器執行類似于調用Object.equals(Object)的比較。 實際上,其比較功能依賴于底層對象的equals(Object)實現的使用。 這意味著最后兩個示例將通過,因為所比較的數字在邏輯上是等效的。 當想要確保更大的身份相等性時(實際上是相同的對象,而不僅僅是相同的邏輯內容),可以使用Hamcrest sameInstance匹配器,如下面的代碼清單所示。 因為斷言是正確的,所以也應用了not匹配器,因為只有預期的和實際的結果不是相同的實例,因此測試僅在“不”的情況下通過測試!

使用Hamcrest sameInstance()和not()

/*** Test of multiplyIntegers method, of class IntegerArithmetic, using core* Hamcrest matchers not and sameInstance.*/@Testpublic void testWithJUnitHamcrestNotSameInstance(){final int[] integers = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15;final int result = this.instance.multiplyIntegers(2, 3, integers);assertThat(result, not(sameInstance(expectedResult)));}

有時需要控制從失敗的單元測試的斷言中輸出的文本。 JUnit包括核心Hamcrest匹配器asDescribed()來支持此功能。 下一個清單中顯示了這樣的代碼示例,代碼清單后的NetBeans IDE的屏幕快照中顯示了失敗測試(和相應的斷言)的輸出。

將Hamcrest asDescribed()與sameInstance()一起使用

/*** Test of multiplyIntegers method, of class IntegerArithmetic, using core* Hamcrest matchers sameInstance and asDescribed. This one will assert a* failure so that the asDescribed can be demonstrated (don't do this with* your unit tests as home)!*/@Testpublic void testWithJUnitHamcrestSameInstanceDescribedAs(){final int[] integers = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};final int expectedResult = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 *13 * 14 * 15;final int result = this.instance.multiplyIntegers(2, 3, integers);assertThat(result,describedAs("Not same object (different identity reference)",sameInstance(expectedResult)));}

當關聯的單元測試斷言失敗時,使用describedAs()允許報告更有意義的消息。

現在,我將使用另一個人為設計的類來幫助說明最新版本的JUnit可用的其他核心Hamcrest匹配器。 接下來顯示“需要測試”。

SetFactory.java

package dustin.examples;import java.lang.reflect.InvocationTargetException; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger;/*** A Factory that provides an implementation of a Set interface based on* supplied SetType that indicates desired type of Set implementation.* * @author Dustin*/ public class SetFactory<T extends Object> {public enum SetType{ENUM(EnumSet.class),HASH(HashSet.class),SORTED(SortedSet.class), // SortedSet is an interface, not implementationTREE(TreeSet.class),RANDOM(Set.class); // Set is an interface, not a concrete collectionprivate Class setTypeImpl = null;SetType(final Class newSetType){this.setTypeImpl = newSetType;}public Class getSetImplType(){return this.setTypeImpl;}}private SetFactory() {}public static SetFactory newInstance(){return new SetFactory();}/*** Creates a Set using implementation corresponding to the provided Set Type* that has a generic parameterized type of that specified.* * @param setType Type of Set implementation to be used.* @param parameterizedType Generic parameterized type for the new set.* @return Newly constructed Set of provided implementation type and using* the specified generic parameterized type; null if either of the provided* parameters is null.* @throws ClassCastException Thrown if the provided SetType is SetType.ENUM,* but the provided parameterizedType is not an Enum.*/public Set<T> createSet(final SetType setType, final Class<T> parameterizedType){if (setType == null || parameterizedType == null){return null;}Set<T> newSet = null;try{switch (setType){case ENUM:if (parameterizedType.isEnum()){newSet = EnumSet.noneOf((Class<Enum>)parameterizedType);}else{throw new ClassCastException("Provided SetType of ENUM being supplied with "+ "parameterized type that is not an enum ["+ parameterizedType.getName() + "].");}break;case RANDOM:newSet = LinkedHashSet.class.newInstance();break;case SORTED:newSet = TreeSet.class.newInstance();break;default:newSet = (Set<T>) setType.getSetImplType().getConstructor().newInstance();break;}}catch ( InstantiationException| IllegalAccessException| IllegalArgumentException| InvocationTargetException| NoSuchMethodException ex){Logger.getLogger(SetFactory.class.getName()).log(Level.SEVERE, null, ex);}return newSet;} }

剛剛顯示了代碼的人為的類提供了使用其他Hamcrest“核心”匹配器的機會。 如上所述,可以將所有這些匹配與is匹配器一起使用,以提高語句的流暢性。 兩個有用的“核心”的匹配是nullValue()和notNullValue()這兩者在未來基于JUnit的代碼列表被證實(和is結合使用在一種情況下)。

使用Hamcrest nullValue()和notNullValue()

/*** Test of createSet method, of class SetFactory, with null SetType passed.*/@Testpublic void testCreateSetNullSetType(){final SetFactory factory = SetFactory.newInstance();final Set<String> strings = factory.createSet(null, String.class);assertThat(strings, nullValue());}/*** Test of createSet method, of class SetFactory, with null parameterized type* passed.*/@Testpublic void testCreateSetNullParameterizedType(){final SetFactory factory = SetFactory.newInstance();final Set<String> strings = factory.createSet(SetType.TREE, null);assertThat(strings, is(nullValue()));}@Testpublic void testCreateTreeSetOfStringsNotNullIfValidParams(){final SetFactory factory = SetFactory.newInstance();final Set<String> strings = factory.createSet(SetType.TREE, String.class);assertThat(strings, notNullValue());}

Hamcrest匹配器instanceOf也很有用,并在下一個代碼清單中進行演示(一個單獨使用instanceOf的示例和一個與is一起使用的示例)。

使用Hamcrest instanceOf()

@Testpublic void testCreateTreeSetOfStringsIsTreeSet(){final SetFactory factory = SetFactory.newInstance();final Set<String> strings = factory.createSet(SetType.TREE, String.class);assertThat(strings, is(instanceOf(TreeSet.class)));}@Testpublic void testCreateEnumSet(){final SetFactory factory = SetFactory.newInstance();final Set<RoundingMode> roundingModes = factory.createSet(SetType.ENUM, RoundingMode.class);roundingModes.add(RoundingMode.UP);assertThat(roundingModes, instanceOf(EnumSet.class));}

到目前為止,許多Hamcrest核心匹配器都提供了更高的流暢性和可讀性,但出于更多原因,我喜歡接下來的兩個匹配器。 Hamcrest hasItem()匹配器檢查集合中指定項的存在,更有用的Hamcrest hasItems()匹配器檢查集合中多個規定項的存在。 在代碼中更容易看到這一點,下面的代碼演示了它們的實際作用。

使用Hamcrest hasItem()和hasItems()

@Testpublic void testCreateTreeSetOfStringsHasOneOfAddedStrings(){final SetFactory factory = SetFactory.newInstance();final Set<String> strings = factory.createSet(SetType.TREE, String.class);strings.add("Tucson");strings.add("Arizona");assertThat(strings, hasItem("Tucson"));}@Testpublic void testCreateTreeSetOfStringsHasAllOfAddedStrings(){final SetFactory factory = SetFactory.newInstance();final Set<String> strings = factory.createSet(SetType.TREE, String.class);strings.add("Tucson");strings.add("Arizona");assertThat(strings, hasItems("Tucson", "Arizona"));}

有時需要測試某種測試方法的結果,以確保它滿足各種各樣的期望。 這就是Hamcrest allOf匹配器派上用場的地方。 此匹配器確保所有條件(表示為匹配器)都為真。 在下面的代碼清單中對此進行了說明,該清單測試了一個斷言,即所生成的Set不為null,具有兩個特定的String且是TreeSet的實例。

使用Hamcrest allOf()

@Testpublic void testCreateSetAllKindsOfGoodness(){final SetFactory factory = SetFactory.newInstance();final Set<String> strings = factory.createSet(SetType.TREE, String.class);strings.add("Tucson");strings.add("Arizona");assertThat(strings,allOf(notNullValue(), hasItems("Tucson", "Arizona"), instanceOf(TreeSet.class)));}

為了演示Hamcrest核心“ anyOf”匹配器提供了現成的JUnit更新版本,我將使用另一個需要單元測試的可笑的Java類。

今天.java

package dustin.examples;import java.util.Calendar; import java.util.Locale;/*** Provide what day of the week today is.* * @author Dustin*/ public class Today {/*** Provide the day of the week of today's date.* * @return Integer representing today's day of the week, corresponding to* static fields defined in Calendar class.*/public int getTodayDayOfWeek(){return Calendar.getInstance(Locale.US).get(Calendar.DAY_OF_WEEK);} }

現在,我需要測試上述類中的唯一方法是否正確返回了代表星期幾的有效整數。 我希望我的測試確保返回代表星期天到星期六的一天的有效整數,但是要測試的方法是這樣的,使得它可能與任何給定的測試運行都不是一周中的同一天。 下面的代碼清單指示如何使用包含JUnit的Hamcrest“ anyOf”匹配器進行測試。

使用Hamcrest anyOf()

/*** Test of getTodayDayOfWeek method, of class Today.*/@Testpublic void testGetTodayDayOfWeek(){final Today instance = new Today();final int todayDayOfWeek = instance.getTodayDayOfWeek();assertThat(todayDayOfWeek,describedAs("Day of week not in range.",anyOf(is(Calendar.SUNDAY),is(Calendar.MONDAY),is(Calendar.TUESDAY),is(Calendar.WEDNESDAY),is(Calendar.THURSDAY),is(Calendar.FRIDAY),is(Calendar.SATURDAY))));}

盡管Hamcrest的allOf要求所有條件都必須匹配才能避免斷言,但任何一個條件的存在足以確保anyOf不會導致失敗的斷言。

我最喜歡的確定JUnit可用的核心Hamcrest匹配器的方法是在Java IDE中使用導入完成。 當我靜態導入org.hamcrest.CoreMatchers.*軟件包內容時,將顯示所有可用的匹配器。 我可以在IDE中查看*代表什么,以查看對我可用的匹配器。

JUnit包含Hamcrest“核心”匹配器真是太好了,這篇文章試圖演示其中的大多數。 Hamcrest在“核心”之外還提供了許多有用的匹配器,它們也很有用。 有關詳細信息,請參見Hamcrest教程 。

參考:在Inspired by Actual Events博客上,我們的JCG合作伙伴 Dustin Marx提供了JUnit的內置Hamcrest Core Matcher支持 。


翻譯自: https://www.javacodegeeks.com/2012/06/junits-built-in-hamcrest-core-matcher.html

idea內置junit5

總結

以上是生活随笔為你收集整理的idea内置junit5_JUnit的内置Hamcrest Core Matcher支持的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 一二级毛片| 欧美日韩成人一区 | 闺蜜张开腿让我爽了一夜 | 四虎网站在线观看 | 极品色影视| 精品妇女一区二区三区 | 亚洲成人午夜在线 | 91网站在线观看视频 | 久久成人国产精品入口 | 色呦呦呦呦 | 日本无翼乌邪恶大全彩h | 欧美特黄一区二区三区 | 久一在线 | 蓝牛av| 国产精品久久久久不卡 | 黄色a级片视频 | 经典av在线| 毛片在线免费观看视频 | 国产日本精品 | 日本特级黄色 | 在线观看中文字幕 | 国产美女特级嫩嫩嫩bbb | 国产精品伦一区二区 | 日女人网站| 欧美精品v国产精品v日韩精品 | 色乱码一区二区三区熟女 | 久久久久无码精品国产sm果冻 | 中文字幕免费在线观看视频 | 久久久高清视频 | 欧洲自拍偷拍 | 色多多污| 日本黄页视频 | 国产精品美乳在线观看 | 国产乱码精品一区二三赶尸艳谈 | 兔费看少妇性l交大片免费 日韩高清不卡 | 无码成人精品区在线观看 | 欧美黑人性猛交xxxx | 日韩一区二区三区电影 | 欧美在线视频免费观看 | 日韩尤物| 亚洲一区二区自拍偷拍 | 狠狠干在线观看 | 国产网站无遮挡 | 另类激情综合 | 成年人在线免费 | 日本老师巨大bbw丰满 | 亚洲第1页| 亚洲少妇18p| 免费看裸体网站视频 | 天天色天天射综合网 | 夜夜嗨av一区二区三区免费区 | 天天摸天天舔 | 亚洲欧美自拍视频 | 逼逼av网站 | 好吊视频一区二区三区 | 粉嫩一区二区三区 | 一区二区三区视频在线观看 | www狠狠 | 欧美午夜精品一区 | 日韩中文字幕在线 | 久久在线视频精品 | 夫妻黄色片 | 欧美黄页在线观看 | 天天做天天爱天天爽 | 一本到免费视频 | 日韩啊v| 蜜臀久久99精品久久久久久宅男 | 日本美女啪啪 | 国产aaa大片 | 日韩av自拍偷拍 | 麻豆久久久午夜一区二区 | 美女毛片视频 | 香蕉久久av一区二区三区 | 色黄视频在线观看 | 国产一级在线免费观看 | 玉女心经 在线 | 婷婷午夜精品久久久久久性色av | 中文字幕一区二区人妻电影 | 青青青免费在线视频 | 日韩七区| 97超碰成人 | 东京热加勒比无码少妇 | 伦理av在线 | 亚洲激情区 | 欧美日本免费 | 久久婷婷影院 | 亚洲欧洲综合网 | 日韩在线一二三 | 国产呦系列 | 日韩欧美一区在线 | 日韩三级视频在线观看 | 日本www在线播放 | 超碰在线98| 久久88 | 日韩天堂av | 久草福利网 | 黄色va | 黄色3级视频 | 亚洲狠狠爱 |