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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hamcrest使用

發(fā)布時(shí)間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hamcrest使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

What is Hamcrest?

什么是Hamcrest?

??Hamcrest is a library of matchers, which can be combined in to create flexible expressions of intent in tests. They've also been used for other purposes.

??Hamcrest 是一個(gè)為了測(cè)試為目的,且能組合成靈活表達(dá)式的匹配器類庫。他們也被用于其他用途。?

Source

To build, please read BUILDING.txt

源碼的構(gòu)建,請(qǐng)閱讀BUILDING.txt

Born in Java, Hamcrest now has implementations in a number of languages.

注意:因?yàn)槲谊P(guān)注的是Java,所以我選擇的是Java Hamcrest,但是Hamcrest還有其它語言的版本。

官網(wǎng)截圖:

Java Hamcrest Binaries

上圖中我已經(jīng)將紅框內(nèi)的jar包全部下載下來了,CSDN的下載鏈接為:http://download.csdn.net/detail/fanxiaobin577328725/9658917

你也可以到上圖的鏈接下載:http://search.maven.org/#search|ga|1|g%3Aorg.hamcrest

explain

1. Introduction
??Hamcrest consists of different jars matching the different needs of applications. This document describes these distributables and the functionality contained in each of them.

??Hamcrest由不同的jar包來匹配應(yīng)用程序的不同的需求。這篇文檔描述了這些包的功能和使用。
2. Overview of distributables
2.1. hamcrest-core.jar

??This is the core API to be used by third-party framework providers. This includes a foundation set of matcher implementations for common operations. This API is stable and will rarely change. You will need this library as a minimum.

??這是使用第三方框架所必須的核心API。這個(gè)Jar包中包含了一組基本的匹配器來執(zhí)行一般的操作。這個(gè)API是穩(wěn)定的,并且很少會(huì)改變。這是你所需要的最基本的庫。
2.2. hamcrest-library.jar
??The ever-growing library of Matcher implementations which are based on the core functionality in hamcrest-core.jar. This will grow between releases.

這是一個(gè)逐漸增加的匹配器庫,這些匹配器的實(shí)現(xiàn)是基于hamcrest-core.jar的核心功能。這將逐漸變化在releases版本之間。

2.3. hamcrest-generator.jar
??A tool to allow many Matcher implementations to be combined into a single class with static methods returning the different matchers so users don't have to remember many classes/packages to import. Generates code. This library is only used internally at compile time. It is not necessary for the use of any of the other hamcrest libraries at runtime.

??這是一個(gè)工具,允許實(shí)現(xiàn)多個(gè)匹配器組合成一個(gè)類并通過靜態(tài)方法返回不同的匹配器,因此使用者沒必要記住很多類/包的名稱來導(dǎo)入了。這個(gè)庫是在編譯時(shí)只在內(nèi)部使用。在運(yùn)行的時(shí)候沒有必要使用其他hamcrest庫。

2.4. hamcrest-integration.jar
??Provides integration between Hamcrest and other testing tools, including JUnit (3 and 4), TestNG, jMock and EasyMock. Uses hamcrest-core.jar and hamcrest-library.jar.

??在使用hamcrest-core.jar and hamcrest-library.jar這兩個(gè)包時(shí),該包提供了它們和其他測(cè)試工具之間的集成,包括JUnit(3和4),TestNG,jMock和EasyMock。

2.5. hamcrest-all.jar
??One jar containing all classes of all the other jars.

??這一個(gè)Jar包包含了其它Jar包中的所有類。
2.6. Maven-Versions
??The description above also applies to the hamcrest Maven artifacts. The dependencies of the jars (hamcrest-integration uses hamcrest-library which uses hamcrest-core)is represented by the Maven dependency mechanism. There is no hamcrest-all library in the Maven repo. Just use hamcrest-integration which references all the other hamcrest libraries.

??上面所描述的Jar包同樣適用于Maven組件。這些Jar包的依賴性(hamcrest-integration依賴于hamcrest-library并且還依賴于hamcrest-core)可以使用Maven依賴機(jī)制。在Maven repo中沒有hamcrest-all這個(gè)庫。只使用hamcrest-integration引用其他hamcrest庫。

Source Repository

托管平臺(tái)網(wǎng)址:https://github.com/hamcrest/JavaHamcrest

Extensions

托管平臺(tái)網(wǎng)址:https://github.com/hamcrest/JavaHamcrest/wiki/Related-Projects

前言:下文是翻譯自Hamcrest官網(wǎng)Documentation的Getting Started,下文中藍(lán)色字體表示我還未翻譯,或者說翻譯不了的部分,如果哪位可以幫助翻譯一下,那真是萬分感謝。

The Hamcrest Tutorial(Hamcrest教程)

1. Introduction(介紹)

??Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluble, such as UI validation, or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used. This tutorial shows you how to use Hamcrest for unit testing.

??本教程向您展示了如何使用hamcres進(jìn)行t單元測(cè)試。

??When writing tests it is sometimes difficult to get the balance right between overspecifying the test (and making it brittle to changes), and not specifying enough (making the test less valuable since it continues to pass even when the thing being tested is broken). Having a tool that allows you to pick out precisely the aspect under test and describe the values it should have, to a controlled level of precision, helps greatly in writing tests that are "just right".Such tests fail when the behaviour of the aspect under test deviates from the expected behaviour, yet continue to pass when minor, unrelated changes to the behaviour are made.
??當(dāng)編寫測(cè)試時(shí),我們很難在編寫測(cè)試用例的時(shí)候保持很好的平衡。測(cè)試用例編寫的過于詳細(xì)會(huì)使得我們難以進(jìn)行修改,測(cè)試用例編寫不詳細(xì),會(huì)導(dǎo)致本不應(yīng)該測(cè)試通過的情況通過了,這就導(dǎo)致測(cè)試失去了其價(jià)值。

2. My first Hamcrest test(第一個(gè)Hamcrest實(shí)例)

??We'll start by writing a very simple JUnit 3 test, but instead of using JUnit'sassertEquals methods,we use Hamcrest'sassertThat construct and the standard set of matchers, both of which westatically import:

??我們先寫一個(gè)簡(jiǎn)單的JUnit3測(cè)試用例,但是我們不適用JUnit的assertEquals方法,而是使用Hamcrest的構(gòu)建的assertThat方法和匹配規(guī)則,但是這需要靜態(tài)導(dǎo)入這兩部分:

import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.*;import junit.framework.TestCase; public class BiscuitTest extends TestCase { public void testEquals() { Biscuit theBiscuit = new Biscuit("Ginger"); Biscuit myBiscuit = new Biscuit("Ginger"); assertThat(theBiscuit, equalTo(myBiscuit)); }}

??The assertThat method is a stylized sentence for making a testassertion. In this example, the subject of the assertion is the objectbiscuitthat is the first method parameter. The second method parameter is a matcher forBiscuitobjects, here a matcher that checks one object is equal to another using theObject equals method. The test passes since theBiscuitclass defines an equals method.

??assertThat方法是生成測(cè)試斷言的一個(gè)固定方法,在這個(gè)例子中,斷言的主體是assertThat方法的第一Biscuit對(duì)象參數(shù),第二個(gè)參數(shù)是一個(gè)Biscuit對(duì)象的匹配器,這個(gè)匹配器(matcher)使用equals方法來檢查第一個(gè)對(duì)象是否等于另外一個(gè)對(duì)象。這個(gè)測(cè)試用例在Biscuit類定義了equals就可以通過測(cè)試。

下面是針對(duì)上面的一個(gè)自我測(cè)試:

源碼追蹤:(只截取了部分源碼)

MatcherAssert:

public class MatcherAssert { public static <T> void assertThat(T actual, Matcher<? super T> matcher) { assertThat("", actual, matcher); } public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) { if (!(matcher.matches(actual))) { Description description = new StringDescription(); description.appendText(reason).appendText("\nExpected: ") .appendDescriptionOf(matcher).appendText("\n but: "); matcher.describeMismatch(actual, description); throw new AssertionError(description.toString()); } }.......}

Matchers:

public class Matchers {....... public static <T> Matcher<T> equalTo(T operand) {?? ??? ?return IsEqual.equalTo(operand);?? ?}.......}

IsEqual:

public class IsEqual<T> extends BaseMatcher<T> { private final Object expectedValue; public IsEqual(T equalArg) { this.expectedValue = equalArg; }....... public boolean matches(Object actualValue) {?? ??? ?return areEqual(actualValue, this.expectedValue);?? ?}....... private static boolean areEqual(Object actual, Object expected) {?? ??? ?if (actual == null) {?? ??? ??? ?return (expected == null);?? ??? ?} ?? ??? ?if ((expected != null) && (isArray(actual))) {?? ??? ??? ?return ((isArray(expected)) && (areArraysEqual(actual, expected)));?? ??? ?} ?? ??? ?return actual.equals(expected);?? ?}........ @Factory?? ?public static <T> Matcher<T> equalTo(T operand) {?? ??? ?return new IsEqual(operand);?? ?}......}

執(zhí)行過程:myBiscuit先傳遞給equalTo(),然后又傳遞給了IsEqual.equalTo(),然后根據(jù)myBiscuit構(gòu)造一個(gè)IsEqual(暫稱為M),并將M返回給assertThat(),assertThat()方法又調(diào)用M.matches(theBiscuit )來進(jìn)行比較,然而M將theBiscuit傳遞給了areEqual()方法,M的areEqual()調(diào)用的就是theBiscuit的equals()方法來進(jìn)行比較myBiscuit的。

饒了一大圈,總的來說就是:比較這兩個(gè)對(duì)象就是調(diào)用第一個(gè)參數(shù)的equals()方法,所以第一個(gè)參數(shù)的equals()方法的比較方式?jīng)Q定了這個(gè)斷言是否通過。

??If you have more than one assertion in your test you caninclude an identifier for the tested value in the assertion:
??如果你有多個(gè)斷言在你的測(cè)試中,你可以為你的每個(gè)斷言聲明一個(gè)標(biāo)識(shí)符,這樣出錯(cuò)也會(huì)知道是那個(gè)斷言拋出了。

assertThat("chocolate chips", theBiscuit.getChocolateChipCount(),equalTo(10));assertThat("hazelnuts", theBiscuit.getHazelnutCount(), equalTo(3));

3. Other test frameworks(其它測(cè)試框架)

?

??Hamcrest has been designed from the outset to integrate with different unit testing frameworks. For example, Hamcrest can be used with JUnit 3 and 4 and TestNG. (For details have a look at the examples that come with the full Hamcrest distribution.) It is easy enough to migrate to using Hamcrest-style assertions in an existing test suite, since other assertion styles can co-exist with Hamcrest's.

??Hamcrest從一開始就設(shè)計(jì)成與不同的單元測(cè)試框架的進(jìn)行整合,例如Hamcrest可以與JUnit 3和4 以及TestNG進(jìn)行整合。(更多細(xì)節(jié)可以參考come with the full Hamcrest distribution)這就很容易遷移到現(xiàn)有的使用Hamcrest格式的測(cè)試框架,只要框架的斷言可以與Hamcrest的斷言共存。

??Hamcrest can also be used with mock objects frameworks by using adaptors to bridge from the mock objects framework's concept of a matcher to a Hamcrest matcher. For example, JMock 1's constraints are Hamcrest's matchers. Hamcrest provides a JMock 1 adaptor to allow you to use Hamcrest matchers in your JMock 1 tests. JMock 2 doesn't need such an adaptor layer since it is designed to use Hamcrest as its matching library. Hamcrest also provides adaptors for EasyMock 2.Again, see the Hamcrest examples for more details.
??Hamcrest也可以通過適配器來適配mock objects(模擬對(duì)象)框架的匹配器,例如,JMock 1的約束是Hamcrest的匹配器,JMock 2就不需要這樣一個(gè)適配器層,只要其被設(shè)計(jì)為使用Hamcrest作為其匹配庫。Hamcrest也為EasyMock 2提供了適配器。

4. A tour of common matchers(常見的匹配器)

Hamcrest comes with a library of useful matchers. Here are some of the most important ones.

Hamcrest自帶了一個(gè)有用的匹配器庫。下面是主要的幾個(gè):

Core

  • anything - always matches, useful if you don't care what the object under test is
  • describedAs - decorator to adding custom failure description
  • is - decorator to improve readability - see "Sugar", below

?Logical

  • allOf - matches if all matchers match, short circuits (like Java &&)
  • anyOf - matches if any matchers match, short circuits (like Java ||)
  • not - matches if the wrapped matcher doesn't match and vice versa

Object

  • equalTo - test object equality using Object.equals
  • hasToString - test Object.toString
  • instanceOf, isCompatibleType - test type
  • notNullValue, nullValue - test for null
  • sameInstance - test object identity

?Beans

  • hasProperty - test JavaBeans properties

?Collections

  • array - test an array's elements against an array of matchers
  • hasEntry, hasKey, hasValue - test a map contains an entry, key or value
  • hasItem, hasItems - test a collection contains elements
  • hasItemInArray - test an array contains an element

Number

  • closeTo - test floating point values are close to a given value
  • greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo - test ordering

Text

  • equalToIgnoringCase - test string equality ignoring case
  • equalToIgnoringWhiteSpace - test string equality ignoring differences in runs of whitespace
  • containsString, endsWith, startsWith - test string matching

5. Sugar

??Hamcrest strives to make your tests as readable as possible. For example, theis matcher is a wrapper that doesn't add any extra behavior to the underlying matcher. The following assertions are all equivalent:

??Hamcrest致力于增強(qiáng)測(cè)試用例的可讀性。例如, 匹配器是一個(gè)包裝,不添加任何額外的底層匹配器。下面的斷言的作用是完全相同的:

assertThat(theBiscuit, equalTo(myBiscuit)); assertThat(theBiscuit, is(equalTo(myBiscuit))); assertThat(theBiscuit, is(myBiscuit));

??The last form is allowed since is(T value) is overloaded to return is(equalTo(value)).

??最后一種方式

6. Writing custom matchers(編寫自定義的匹配器)

??Hamcrest comes bundled with lots of useful matchers, but you'll probably find that you need to create your own from time to time to fit your testing needs. This commonly occurs when you find a fragment of code that tests the same set of properties over and over again (and in different tests), and you want to bundle the fragment into a single assertion. By writing your own matcher you'll eliminate code duplication and make your tests more readable!

??Hamcrest綁定了許多有用的匹配器,但是有時(shí)候我們需要?jiǎng)?chuàng)建自己的匹配器來滿足我們測(cè)試的需要。這種情況通常發(fā)生在我們使用一段代碼片段并且反復(fù)測(cè)試相同的屬性集(在不同的測(cè)試中),我們又想把這個(gè)代碼片段打包成一個(gè)斷言。通過編寫自己的匹配器我們會(huì)減少代碼的重復(fù)性,使測(cè)試更加具有可讀性!

??Let's write our own matcher for testing if a double value has the value NaN (not a number). This is the test we want to write:
??如果double類型的之有NaN值(不是一個(gè)數(shù)字),讓我們?yōu)檫@種情況寫一個(gè)我們自己的匹配器,。下面使我們想寫的測(cè)試:

public void testSquareRootOfMinusOneIsNotANumber() { assertThat(Math.sqrt(-1), is(notANumber())); }

And here's the implementation:

下面是其實(shí)現(xiàn):

?

package org.hamcrest.examples.tutorial; import org.hamcrest.Description;import org.hamcrest.Factory;import org.hamcrest.Matcher;import org.hamcrest.TypeSafeMatcher; public class IsNotANumber extends TypeSafeMatcher{ @Override public boolean matchesSafely(Double number){ return number.isNaN(); } public void describeTo(Description description){ description.appendText("not a number"); } @Factory public static Matcher notANumber(){ return new IsNotANumber(); }}

??The assertThat method is a generic method which takes aMatcher parameterized by the type of the subject of the assertion. We are asserting things about Double values, so we know that we need a Matcher<Double>. For our Matcher implementation it is most convenient to subclass TypeSafeMatcher,which does the cast to a Double for us. We need only implement thematchesSafely method - which simply checks to see if the Double is NaN - and the describeTo method - which is used to produce a failure message when a test fails. Here's an example of how the failure message looks:
??assertThat方法是一個(gè)通用的匹配方法,其需要一個(gè)匹配器類型的參數(shù),而這個(gè)參數(shù)是一個(gè)斷言的形式。我們匹配的是一個(gè)Double類型的值,所以我們需要一個(gè)Matcher<Double>的匹配器。我們的匹配器繼承TypeSafeMatcher是最為方便的,這樣我們就可以測(cè)試Double這種情況了。我們只需要復(fù)寫matchesSafely ()方法(這個(gè)方法就是簡(jiǎn)單檢查Double值是否是NaN),describeTo ()方法是用來產(chǎn)生一個(gè)測(cè)試失敗時(shí)失敗消息的。下面的例子就是演示錯(cuò)誤信息的顯示地:

assertThat(1.0, is(notANumber()));

fails with the message(失敗的信息)

java.lang.AssertionError: Expected: is not a number got : <1.0>

??The third method in our matcher is a convenience factory method. We statically import this method to use the matcher in our test:
??我們匹配器中的第三種方法就是一個(gè)方便的工廠方法。在測(cè)試中我們通過靜態(tài)導(dǎo)入這個(gè)方法的方式來使用我們的匹配器:

import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.*;import static org.hamcrest.examples.tutorial.IsNotANumber.notANumber;import junit.framework.TestCase; public class NumberTest extends TestCase{ public void testSquareRootOfMinusOneIsNotANumber() { assertThat(Math.sqrt(-1), is(notANumber())); }}

??Even though the notANumber method creates a new matcher each time it is called, you should not assume this is the only usage pattern for your matcher. Therefore you should make sure your matcher is stateless,so a single instance can be reused between matches.
??即使notANumber()方法在每次被調(diào)用的時(shí)候都會(huì)創(chuàng)建一個(gè)新的匹配器,你不應(yīng)該認(rèn)為這是唯一使用的模式匹配器。因此我們應(yīng)該確保我們的匹配器是stateless,因此單獨(dú)的實(shí)例可以再多個(gè)匹配器間重復(fù)使用。

6. Sugar generation

??If you produce more than a few custom matchers it becomes annoying to have to import them all individually. It would be nice to be able to group them together in a single class, so they can be imported using a single static import much like the Hamcrest library matchers. Hamcrest helps out here by providing a way to do this by using a generator.
??如果你定義了很多自定義的匹配器,這會(huì)導(dǎo)致我們必須分別的單獨(dú)導(dǎo)入它們,這讓人很煩惱。最好是把它們組織到一個(gè)類中,這樣它們就可以像導(dǎo)入Hamcrest匹配器庫一樣,靜態(tài)導(dǎo)入這一個(gè)類就可以了。Hamcrest提供了一種方式來完成這種情況,那就是使用一個(gè)generator。
??First, create an XML configuration file listing all the Matcher classes that should be searched for factory methodsannotated with the org.hamcrest.Factory annotation. For example:

??首先,創(chuàng)建一個(gè)XML的配置文件來羅列出我們所有的匹配器類應(yīng)該尋找的工廠方法(),如下面的例子:(官網(wǎng)上是空的)

??Second, run the org.hamcrest.generator.config.XmlConfigurator command-line tool that comes with Hamcrest. This tool takes the XML configuration file and generates a single Java class that includes all the factory methods specified by the XML file. Running it with no arguments will display a usage message. Here's the output for the example.
??第二步運(yùn)行,在控制臺(tái)運(yùn)行org.hamcrest.generator.config.XmlConfigurator。這個(gè)工具將根據(jù)XML配置文件生成一個(gè)Java類,其包含了所有的XML文件中指定的工廠方法。不帶參數(shù)的運(yùn)行它,將會(huì)顯示一個(gè)使用信息,這里有一個(gè)輸出的例子:

// Generated source. package org.hamcrest.examples.tutorial;public class Matchers{ public static org.hamcrest.Matcher is(T param1){ return org.hamcrest.core.Is.is(param1); } public static org.hamcrest.Matcher is(java.lang.Class param1) { return org.hamcrest.core.Is.is(param1); } public static org.hamcrest.Matcher is(org.hamcrest.Matcher param1) { return org.hamcrest.core.Is.is(param1); } public static org.hamcrest.Matcher notANumber(){ return org.hamcrest.examples.tutorial.IsNotANumber.notANumber(); }}

Finally, we can update our test to use the new Matchers class.
最后,我們可以更新我們的測(cè)試用例,來使用新定義的匹配器類了。

import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.examples.tutorial.Matchers.*;import junit.framework.TestCase;public class CustomSugarNumberTest extends TestCase{ public void testSquareRootOfMinusOneIsNotANumber(){ assertThat(Math.sqrt(-1), is(notANumber())); }}

Notice we are now using the Hamcrest library is matcher imported from our own custom Matchersclass.
請(qǐng)注意我們現(xiàn)在使用Hamcrest庫是我們自定義的匹配器類進(jìn)口匹配器。

7. Where next?

See FurtherResources.

參考資料:

  • JUnit4---Hamcrest匹配器常用方法總結(jié)
原文地址:https://blog.csdn.net/fanxiaobin577328725/article/details/52862945

轉(zhuǎn)載于:https://www.cnblogs.com/jpfss/p/10955919.html

總結(jié)

以上是生活随笔為你收集整理的Hamcrest使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。