当前位置: 首页 > news >正文

怎么自己搭建网站购物网站哪里建最好

怎么自己搭建网站,购物网站哪里建最好,东莞网络优化哪家好,建设部网站撤销注册资质的都是公职人员吗本文是我们名为“ 用Mockito进行测试 ”的学院课程的一部分。 在本课程中#xff0c;您将深入了解Mockito的魔力。 您将了解有关“模拟”#xff0c;“间谍”和“部分模拟”的信息#xff0c;以及它们相应的Stubbing行为。 您还将看到使用测试双打和对象匹配器进行验证的过… 本文是我们名为“ 用Mockito进行测试 ”的学院课程的一部分。 在本课程中您将深入了解Mockito的魔力。 您将了解有关“模拟”“间谍”和“部分模拟”的信息以及它们相应的Stubbing行为。 您还将看到使用测试双打和对象匹配器进行验证的过程。 最后讨论了使用Mockito的测试驱动开发TDD以了解该库如何适合TDD的概念。 在这里查看 在本教程中我们将研究Hamcrest Matcher库以及如何将其与JUnit和Mockito集成。 目录 1.什么是Hamcrest 2.包括Hamcrest 3.认识匹配者 3.1。 简单匹配器 3.2。 简单匹配器结合其他匹配器 4。结论 1.什么是Hamcrest Hamcrest是用于创建匹配对象的框架。 这些匹配器对象是谓词用于编写在某些条件下可以满足的规则。 它们通常用于自动化测试中尽管它们可以用于其他情况下例如数据验证。 Hamcrest让我们超越了简单的JUnit断言并使我们能够编写非常具体可读的验证代码。 Hamcrest旨在使测试更具可读性。 它充分利用静态方法来创建断言语法该语法易于编写和理解。 当与JUnit和Mockito结合使用时它使我们能够编写清晰简洁的测试以满足“测试一件事”的良好单元测试的特性。 假设我们有一个String我们想测试它是否与另一个期望的字符串相等我们可以使用JUnit断言来测试它 assertEquals(expected, actual); 在Hamcrest中我们使用JUnit assertThat(valueUnderTest, matcher)方法。 此方法始终构成Hamcrest断言的基础 我们断言被测值满足匹配谓词。 要在最基本的水平上用hamcrest重写上述测试我们可以编写 assertThat(actual, equalTo(expected)); 注意assertThat约定将要测试的实际值作为第一个参数这与assertEquals约定相反。 这是对可读性的改进但是Hamcrest还以is()匹配器的形式为我们提供了一些不错的语法糖。 该匹配器本身不执行任何操作它只是中继其输入匹配器的结果从而使您的断言代码可以像英语一样读取。 让我们使用is()重写上面的内容 assertThat(actual, is(equalTo(expected))); 很好很可读 Hamcrest的匹配器失败时它会生成详细的输出并指定期望值和实际值以帮助您确定测试失败的原因。 查看以下测试用例 Testpublic void test_failed() throws Exception {// GivenInteger number 7;// ThenassertThat(number, greaterThan(10));} 显然该测试将失败但是Hamcrest将提供有关失败的详细信息 java.lang.AssertionError: Expected: a value greater than 10but: 7 was less than 10 在本教程中我们将遵循仅在一个单元测试中包含一个断言的约定。 这似乎是重复的尤其是在许多测试中测试的设置部分相同的情况下但是这是单元测试中的良好做法。 它使我们能够创建针对性的测试-仅当单个断言失败时测试才会失败其他所有断言将继续执行。 它使我们能够创建可读的测试-我们可以一目了然地看到测试的目的。 它使我们能够创建记录代码的测试-我们可以使用表达测试目的的测试名称从而传达测试的详细目的请考虑customer_should_have_balance_updated_by_input_order_amount()而不是verifyOrderMethod() 。 它使我们能够创建不易碎的测试-如果测试做得太多则如果更改了不相关的功能它可能会中断从而迫使我们重写测试只是为了使其再次运行而无需更改实际的被测代码。 如果我们遵循“测试一件事”的习惯那么将来我们将编写出更好的单元测试 2.包括Hamcrest 如果使用的是Maven则可以将Hamcrest添加到您的项目中并且对pom.xml具有以下依赖性 dependencygroupIdorg.hamcrest/groupIdartifactIdhamcrest-all/artifactIdversion1.3/version/dependency 如果您使用的是Gradle请添加以下内容 dependencies {testCompile org.hamcrest:hamcrest-all:1.3} 要将hamcrest直接添加到项目的类路径中可以从https://code.google.com/p/hamcrest/下载hamcrest-all-1.3.jar到硬盘上的某个位置。 右键单击您的eclipse项目然后选择“属性”然后在左窗格中选择“ Java Build Path”然后在右侧选择“ Libraries”。 在“库”选项卡上单击“添加外部罐子”按钮然后导航到先前下载的所有hamcrest罐子。 选择罐子它现在已添加到您的项目中并可供使用。 请注意JUnit与简化版本的HamcrestHamcrest Core捆绑在一起因此如果JUnit出现在类路径上的Hamcrest之前则编译器将选择该版本。 为了解决这个问题请确保Hamcrest在类路径上的JUnit之前出现。 您可以在Maven中通过在所有其他依赖项之前列出hamcrest-all依赖项来实现此目的。 与Mockito静态方法一样我们可以通过启动Window- Preferences并将Hamcrest库添加到Eclipse内容辅助中并转到左侧导航栏中的Java / Editor / Content Assist / Favorites。 之后按照图1添加以下内容作为“ New Type…” org.hamcrest.Matchers 启动Window- Preferences然后转到左侧导航栏中的Java / Editor / Content Assist / Favorites。 之后按照图1添加以下内容作为“ New Type…” 图1 – Content Assist收藏夹 3.认识匹配者 Hamcrest提供了一个静态工厂方法库用于在org.hamcrest.Matchers类中创建Matchers因此您可以通过静态导入引入所有Matchers import static org.hamcrest.Matchers.* 但是如果这样做则存在命名冲突的风险因为Hamcrest和Mockito都提供了静态的any()方法因此建议导入您单独使用的每个静态方法。 现在我们将在Hamcrest Matchers库中查看所有可用的Matchers。 它们将分为两大类 用于测试值的匹配器简单和用于组合其他匹配器聚合的匹配器。 简单匹配器 以下匹配器主要用于测试输入值。 3.1.1。 任何 匹配给定类型的任何变量。 Testpublic void test_any() throws Exception {// GivenString myString hello;// ThenassertThat(myString, is(any(String.class))); } 3.1.2。 任何东西 匹配任何东西。 Testpublic void test_anything() throws Exception {// GivenString myString hello;Integer four 4;// ThenassertThat(myString, is(anything()));assertThat(four, is(anything()));} 3.1.3。 arrayContaining 数组的各种匹配器数组的长度必须匹配匹配器的数量并且它们的顺序很重要。 数组是否按输入到匹配器的顺序包含所有给定项目 Testpublic void test_arrayContaining_items() throws Exception {// GivenString[] strings {why, hello, there};// ThenassertThat(strings, is(arrayContaining(why, hello, there)));} 数组中是否包含按顺序匹配匹配器输入列表的项目 Testpublic void test_arrayContaining_list_of_matchers() throws Exception {// GivenString[] strings {why, hello, there};// Thenjava.util.Listorg.hamcrest.Matcher? super String itemMatchers new ArrayList();itemMatchers.add(equalTo(why));itemMatchers.add(equalTo(hello));itemMatchers.add(endsWith(here));assertThat(strings, is(arrayContaining(itemMatchers)));} 数组是否按顺序包含与输入vararg匹配器匹配的项目 Testpublic void test_arrayContaining_matchers() throws Exception {// GivenString[] strings {why, hello, there};// ThenassertThat(strings, is(arrayContaining(startsWith(wh), equalTo(hello), endsWith(here))));} 3.1.4。 arrayContainingInAnyOrder 数组的各种匹配器数组的长度必须匹配匹配器的数量但是它们的顺序并不重要。 数组是否包含所有给定项 Testpublic void test_arrayContainingInAnyOrder_items() throws Exception {// GivenString[] strings { why, hello, there };// ThenassertThat(strings, is(arrayContainingInAnyOrder(hello, there, why)));} 数组中是否包含与Matchers的输入集合匹配的项目 Testpublic void test_arrayContainingInAnyOrder_collection_of_matchers() throws Exception {// GivenString[] strings { why, hello, there };// ThenSetorg.hamcrest.Matcher? super String itemMatchers new HashSet();itemMatchers.add(equalTo(hello));itemMatchers.add(equalTo(why));itemMatchers.add(endsWith(here));assertThat(strings, is(arrayContainingInAnyOrder(itemMatchers)));} 数组是否包含与输入vararg匹配器匹配的项目 Testpublic void test_arrayContainingInAnyOrder_matchers() throws Exception {// GivenString[] strings { why, hello, there };// ThenassertThat(strings, is(arrayContainingInAnyOrder(endsWith(lo), startsWith(the), equalTo(why))));} 3.1.5。 arrayWithSize 各种匹配器用于检查数组是否具有特定长度。 输入数组是否具有指定的长度 Testpublic void test_arrayWithSize_exact() throws Exception {// GivenString[] strings { why, hello, there };// ThenassertThat(strings, is(arrayWithSize(3)));} 输入数组的长度是否与指定的匹配项匹配 Testpublic void test_arrayWithSize_matcher() throws Exception {// GivenString[] strings { why, hello, there };// ThenassertThat(strings, is(arrayWithSize(greaterThan(2))));} 3.1.6。 相近 可以与Double或BigDecimal一起使用的匹配器以检查某个值是否在期望值的指定误差范围内。 双 Testpublic void test_closeTo_double() throws Exception {// GivenDouble testValue 6.3;// ThenassertThat(testValue, is(closeTo(6, 0.5)));} 大十进制 Testpublic void test_closeTo_bigDecimal() throws Exception {// GivenBigDecimal testValue new BigDecimal(324.0);// ThenassertThat(testValue, is(closeTo(new BigDecimal(350), new BigDecimal(50))));} 3.1.7。 comparesEqualTo 创建一个Comparable匹配器尝试使用输入值的compareTo()方法匹配输入匹配器值。 如果compareTo()方法为输入的匹配器值返回0则匹配器将匹配否则将不匹配。 Testpublic void test_comparesEqualTo() throws Exception {// GivenString testValue value;// ThenassertThat(testValue, comparesEqualTo(value));} 3.1.8。 contains 各种匹配器可用于检查输入Iterable是否包含值。 值的顺序很重要并且Iterable中的项目数必须与要测试的值数相匹配。 输入列表是否按顺序包含所有值 Testpublic void test_contains_items() throws Exception {// GivenListString strings Arrays.asList(why, hello, there);// ThenassertThat(strings, contains(why, hello, there));} 输入列表中是否包含按顺序匹配输入匹配器列表中所有匹配器的项目 Testpublic void test_contains_list_of_matchers() throws Exception {// GivenListString strings Arrays.asList(why, hello, there);// ThenListorg.hamcrest.Matcher? super String matchers new ArrayList();matchers.add(startsWith(wh));matchers.add(endsWith(lo));matchers.add(equalTo(there));assertThat(strings, contains(matchers));} 输入列表中是否仅包含与输入匹配项匹配的一项 Testpublic void test_contains_single_matcher() throws Exception {// GivenListString strings Arrays.asList(hello);// ThenassertThat(strings, contains(startsWith(he)));} 输入列表中是否包含按顺序匹配输入vararg匹配器中所有匹配器的项 Testpublic void test_contains_matchers() throws Exception {// GivenListString strings Arrays.asList(why, hello, there);// ThenassertThat(strings, contains(startsWith(why), endsWith(llo), equalTo(there)));} 3.1.9。 containsInAnyOrder 各种匹配器可用于检查输入Iterable是否包含值。 值的顺序并不重要但是Iterable中的项目数必须与要测试的值数相匹配。 输入列表是否以任何顺序包含所有值 Testpublic void test_containsInAnyOrder_items() throws Exception {// GivenListString strings Arrays.asList(why, hello, there);// ThenassertThat(strings, containsInAnyOrder(hello, there, why));} 输入列表中是否包含与输入匹配器列表中的所有匹配器按任何顺序匹配的项目 Testpublic void test_containsInAnyOrder_list_of_matchers() throws Exception {// GivenListString strings Arrays.asList(why, hello, there);// ThenListorg.hamcrest.Matcher? super String matchers new ArrayList();matchers.add(equalTo(there));matchers.add(startsWith(wh));matchers.add(endsWith(lo)); assertThat(strings, containsInAnyOrder(matchers));} 输入列表中是否包含以任何顺序匹配输入vararg匹配器中所有匹配器的项目 Testpublic void test_containsInAnyOrder_matchers() throws Exception {// GivenListString strings Arrays.asList(why, hello, there);// ThenassertThat(strings, containsInAnyOrder(endsWith(llo), equalTo(there), startsWith(why)));} 3.1.10。 containsString 如果被测字符串包含给定的子字符串则匹配的匹配器。 Testpublic void test_containsString() throws Exception {// GivenString testValue value;// ThenassertThat(testValue, containsString(alu));} 3.1.11。 空 如果输入Collections isEmpty()方法返回true则匹配的匹配器。 Testpublic void test_empty() throws Exception {// GivenSetString testCollection new HashSet();// ThenassertThat(testCollection, is(empty()));} 3.1.12。 emptyArray 如果输入数组的长度为0则匹配的匹配器。 Testpublic void test_emptyArray() throws Exception {// GivenString[] testArray new String[0];// ThenassertThat(testArray, is(emptyArray()));} 3.1.13。 emptyCollectionOf 类型安全匹配器如果输入集合为给定类型且为空则匹配。 Testpublic void test_emptyCollectionOf() throws Exception {// GivenSetString testCollection new HashSet();// ThenassertThat(testCollection, is(emptyCollectionOf(String.class)));} 3.1.14。 emptyIterable 如果输入Iterable没有值则匹配的匹配器。 Testpublic void test_emptyIterable() throws Exception {// GivenSetString testCollection new HashSet();// ThenassertThat(testCollection, is(emptyIterable()));} 3.1.15。 emptyIterableOf Typesafe Matcher如果输入的Iterable没有值并且是给定类型则匹配。 Testpublic void test_emptyIterableOf() throws Exception {// GivenSetString testCollection new HashSet();// ThenassertThat(testCollection, is(emptyIterableOf(String.class)));} 3.1.16。 以。。结束 如果输入String以给定的子字符串结尾则匹配的匹配器。 Testpublic void test_endsWith() throws Exception {// GivenString testValue value;// ThenassertThat(testValue, endsWith(lue));} 3.1.17。 等于 如果输入值在逻辑上等于给定测试值则匹配的匹配器。 也可以在数组上使用在这种情况下它将检查数组的长度并确保输入测试数组中的所有值在逻辑上都等于指定数组的值。 单值。 Testpublic void test_equalTo_value() throws Exception {// GivenString testValue value;// ThenassertThat(testValue, equalTo(value));} 数组。 Testpublic void test_equalTo_array() throws Exception {// GivenString[] testValues { why, hello, there };// ThenString[] specifiedValues { why, hello, there };assertThat(testValues, equalTo(specifiedValues));} 3.1.18。 equalToIgnoringCase 如果输入的String值等于指定的String且忽略大小写则匹配的匹配器。 Testpublic void test_equalToIgnoringCase() throws Exception {// GivenString testValue value;// ThenassertThat(testValue, equalToIgnoringCase(VaLuE));} 3.1.19。 equalToIgnoringWhiteSpace 如果输入的String值等于指定的String而不匹配多余的空格则匹配该匹配器。 忽略所有前导和尾随空格并将所有剩余空格折叠为单个空格。 Testpublic void test_equalToIgnoringWhiteSpace() throws Exception {// GivenString testValue this is my value ;// ThenassertThat(testValue, equalToIgnoringWhiteSpace(this is my value));} 3.1.20。 eventFrom 如果输入EventObject来自给定Source则匹配的Matcher。 也可以接受指定子类型的EventObeject 。 Testpublic void test_eventFrom() throws Exception {// GivenObject source new Object();EventObject testEvent new EventObject(source);// ThenassertThat(testEvent, is(eventFrom(source)));} 指定子类型。 Testpublic void test_eventFrom_type() throws Exception {// GivenObject source new Object();EventObject testEvent new MenuEvent(source);// ThenassertThat(testEvent, is(eventFrom(MenuEvent.class, source)));} 3.1.21。 比...更棒 如果输入测试值大于指定值则匹配的匹配器。 Testpublic void test_greaterThan() throws Exception {// GivenInteger testValue 5;// ThenassertThat(testValue, is(greaterThan(3)));} 3.1.22。 GreaterThanOrEqual 如果输入测试值大于或等于指定值则匹配的匹配器。 Testpublic void test_greaterThanOrEqualTo() throws Exception {// GivenInteger testValue 3;// ThenassertThat(testValue, is(greaterThanOrEqualTo(3)));} 3.1.23。 hasEntry 如果给定映射包含与指定键和值匹配的条目则匹配的匹配器或匹配器。 实际值 Testpublic void test_hasEntry() throws Exception {// GivenInteger testKey 1;String testValue one;MapInteger, String testMap new HashMap();testMap.put(testKey, testValue);// ThenassertThat(testMap, hasEntry(1, one));} 匹配器 Testpublic void test_hasEntry_matchers() throws Exception {// GivenInteger testKey 2;String testValue two;MapInteger, String testMap new HashMap();testMap.put(testKey, testValue);// ThenassertThat(testMap, hasEntry(greaterThan(1), endsWith(o)));} 3.1.24。 hasItem 如果输入Iterable具有至少一个与指定值或匹配器匹配的项目则匹配的匹配器。 实际价值 Testpublic void test_hasItem() throws Exception {// GivenListInteger testList Arrays.asList(1,2,7,5,4,8);// ThenassertThat(testList, hasItem(4));} 匹配器 Testpublic void test_hasItem_matcher() throws Exception {// GivenListInteger testList Arrays.asList(1,2,7,5,4,8);// ThenassertThat(testList, hasItem(is(greaterThan(6))));} 3.1.25。 hasItemInArray 如果输入数组具有至少一个与指定值或匹配器匹配的项目则匹配的匹配器。 实际价值 Testpublic void test_hasItemInArray() throws Exception {// GivenInteger[] test {1,2,7,5,4,8};// ThenassertThat(test, hasItemInArray(4));} 匹配器 Testpublic void test_hasItemInArray_matcher() throws Exception {// GivenInteger[] test {1,2,7,5,4,8};// ThenassertThat(test, hasItemInArray(is(greaterThan(6))));} 3.1.26。 hasItems 如果输入Iterable具有任意顺序的所有指定值或匹配器则匹配的匹配器。 实际值 public void test_hasItems() throws Exception {// GivenListInteger testList Arrays.asList(1,2,7,5,4,8);// ThenassertThat(testList, hasItems(4, 2, 5));} 匹配器 Testpublic void test_hasItems_matcher() throws Exception {// GivenListInteger testList Arrays.asList(1,2,7,5,4,8);// ThenassertThat(testList, hasItems(greaterThan(6), lessThan(2)));} 3.1.27。 hasKey 如果输入Map具有至少一个与指定值或匹配器匹配的键则匹配器匹配。 实际价值 Testpublic void test_hasKey() throws Exception {// GivenMapString, String testMap new HashMap();testMap.put(hello, there);testMap.put(how, are you?);// ThenassertThat(testMap, hasKey(hello));} 匹配器 Testpublic void test_hasKey_matcher() throws Exception {// GivenMapString, String testMap new HashMap();testMap.put(hello, there);testMap.put(how, are you?);// ThenassertThat(testMap, hasKey(startsWith(h)));} 3.1.28。 hasProperty 如果输入的Object满足Bean Convention并具有指定名称的属性并且该属性的值可选地与指定的Matcher匹配则匹配该Matcher。 物业名称 Testpublic void test_hasProperty() throws Exception {// GivenJTextField testBean new JTextField();testBean.setText(Hello, World!);// ThenassertThat(testBean, hasProperty(text));} 属性名称和值匹配器 Testpublic void test_hasProperty_value() throws Exception {// GivenJTextField testBean new JTextField();testBean.setText(Hello, World!);// ThenassertThat(testBean, hasProperty(text, startsWith(H)));} 3.1.29。 hasSize 如果输入Collection具有指定的大小或者它的大小与指定的匹配器匹配则匹配器。 实际价值 Testpublic void test_hasSize() throws Exception {// GivenListInteger testList Arrays.asList(1,2,3,4,5);// ThenassertThat(testList, hasSize(5));} 匹配器 Testpublic void test_hasSize_matcher() throws Exception {// GivenListInteger testList Arrays.asList(1,2,3,4,5);// ThenassertThat(testList, hasSize(lessThan(10)));} 3.1.30。 hasToString 如果输入对象的toString方法匹配指定的String或输入匹配器则匹配的匹配器。 正常价值 Testpublic void test_hasToString() throws Exception {// GivenInteger testValue 4;// ThenassertThat(testValue, hasToString(4));} 匹配器 Testpublic void test_hasToString_matcher() throws Exception {// GivenDouble testValue 3.14;// ThenassertThat(testValue, hasToString(containsString(.)));} 3.1.31。 hasValue 如果输入Map具有至少一个与指定值或匹配器匹配的值则匹配的匹配器。 实际价值 Testpublic void test_hasValue() throws Exception {// GivenMapString, String testMap new HashMap();testMap.put(hello, there);testMap.put(how, are you?);// ThenassertThat(testMap, hasValue(there));} 匹配器 Testpublic void test_hasValue_matcher() throws Exception {// GivenMapString, String testMap new HashMap();testMap.put(hello, there);testMap.put(how, are you?);// ThenassertThat(testMap, hasValue(containsString(?)));} 3.1.32。 hasXPath 如果输入XML DOM节点满足输入XPath表达式则匹配的匹配器。 节点是否包含与输入XPath表达式匹配的节点 Testpublic void test_hasXPath() throws Exception {// GivenDocumentBuilder docBuilder DocumentBuilderFactory.newInstance().newDocumentBuilder();Node testNode docBuilder.parse(new InputSource(new StringReader(xmltopmiddlebottomvalue/bottom/middle/top/xml))).getDocumentElement();// ThenassertThat(testNode, hasXPath(/xml/top/middle/bottom));} Node是否包含与输入XPath表达式匹配的Node并且该Node的值是否与指定的匹配器匹配 Testpublic void test_hasXPath_matcher() throws Exception {// GivenDocumentBuilder docBuilder DocumentBuilderFactory.newInstance().newDocumentBuilder();Node testNode docBuilder.parse(new InputSource(new StringReader(xmltopmiddlebottomvalue/bottom/middle/top/xml))).getDocumentElement();// ThenassertThat(testNode, hasXPath(/xml/top/middle/bottom, startsWith(val)));} 节点是否在指定的名称空间中包含与输入XPath表达式匹配的节点 Test public void test_hasXPath_namespace() throws Exception {// GivenDocumentBuilderFactory docFactory DocumentBuilderFactory.newInstance();docFactory.setNamespaceAware(true);DocumentBuilder docBuilder docFactory.newDocumentBuilder();Node testNode docBuilder.parse(new InputSource(new StringReader(xml xmlns:prefixhttp://namespace-uritopmiddleprefix:bottomvalue/prefix:bottom/middle/top/xml))).getDocumentElement();NamespaceContext namespace new NamespaceContext() {public String getNamespaceURI(String prefix) {return http://namespace-uri;}public String getPrefix(String namespaceURI) {return null;}public IteratorString getPrefixes(String namespaceURI) {return null;}};// ThenassertThat(testNode, hasXPath(//prefix:bottom, namespace)); } Node是否在指定的名称空间中包含一个与输入XPath表达式匹配的Node并且该Node的值是否与指定的Matcher匹配 Test public void test_hasXPath_namespace_matcher() throws Exception {// GivenDocumentBuilderFactory docFactory DocumentBuilderFactory.newInstance();docFactory.setNamespaceAware(true);DocumentBuilder docBuilder docFactory.newDocumentBuilder();Node testNode docBuilder.parse(new InputSource(new StringReader(xml xmlns:prefixhttp://namespace-uritopmiddleprefix:bottomvalue/prefix:bottom/middle/top/xml))).getDocumentElement();NamespaceContext namespace new NamespaceContext() {public String getNamespaceURI(String prefix) {return http://namespace-uri;}public String getPrefix(String namespaceURI) {return null;}public IteratorString getPrefixes(String namespaceURI) {return null;}};// ThenassertThat(testNode, hasXPath(//prefix:bottom, namespace, startsWith(val))); } 3.1.33。 instanceOf 如果输入对象是给定类型则匹配器。 Test public void test_instanceOf() throws Exception {// GivenObject string Hello, World!;// ThenassertThat(string, instanceOf(String.class)); } 3.1.34。 isEmptyOrNullString 当输入字符串为空或null时匹配的匹配器。 Test public void test_isEmptyOrNullString() throws Exception {// GivenString emptyString ;String nullString null;// ThenassertThat(emptyString, isEmptyOrNullString());assertThat(nullString, isEmptyOrNullString()); } 3.1.35。 isEmptyString 当输入字符串为空时匹配的匹配器。 Test public void test_isEmptyString() throws Exception {// GivenString emptyString ;// ThenassertThat(emptyString, isEmptyString()); } 3.1.36。 isIn 当在给定的Collection或Array中找到输入项时匹配的匹配器。 Test public void test_isIn() throws Exception {// GivenSetInteger set new HashSet();set.add(3);set.add(6);set.add(4);// ThenassertThat(4, isIn(set)); } 3.1.37。 是其中之一 当输入对象是给定对象之一时匹配的匹配器。 Test public void test_isOneOf() throws Exception {// ThenassertThat(4, isOneOf(3,4,5)); } 3.1.38。 iterableWithSize 当输入Iterable具有指定大小时匹配或与指定大小匹配器匹配的匹配器。 实际价值 Test public void test_iterableWithSize() throws Exception {// GivenSetInteger set new HashSet();set.add(3);set.add(6);set.add(4);// ThenassertThat(set, iterableWithSize(3)); } 匹配器 Test public void test_iterableWithSize_matcher() throws Exception {// GivenSetInteger set new HashSet();set.add(3);set.add(6);set.add(4);// ThenassertThat(set, iterableWithSize(lessThan(4))); } 3.1.39。 少于 匹配器使用compareTo方法匹配输入对象小于指定值的可比较对象。 Test public void test_lessThan() throws Exception {// ThenassertThat(apple, lessThan(zoo)); } 3.1.40。 lessThanOrEqualTo 匹配器使用compareTo方法匹配输入对象小于或等于指定值的可比较对象。 Test public void test_lessThanOrEqualTo() throws Exception {// ThenassertThat(2, lessThanOrEqualTo(2)); } 3.1.41。 不 包裹现有匹配器并反转其匹配逻辑的匹配器 Test public void test_not_matcher() throws Exception {// ThenassertThat(zoo, not(lessThan(apple))); } 当与值而不是匹配器一起使用时也是not(equalTo(...))的别名 Test public void test_not_value() throws Exception {// ThenassertThat(apple, not(orange)); } 3.1.42。 notNullValue 当输入值不为null时匹配的匹配器。 Test public void test_notNullValue() throws Exception {// ThenassertThat(apple, notNullValue()); } 3.1.43。 nullValue 当输入值为null时匹配的匹配器。 Test public void test_nullValue() throws Exception {// GivenObject nothing null;// ThenassertThat(nothing, nullValue()); } 3.1.44。 sameInstance 当输入对象与指定值相同的实例时匹配的匹配器。 Test public void test_sameInstance() throws Exception {// GivenObject one new Object();Object two one;// ThenassertThat(one, sameInstance(two)); } 3.1.45。 samePropertyValuesAs 当输入Bean具有与指定Bean相同的属性值时匹配的匹配项即如果被测Bean上具有属性则它们必须存在并且具有与在测试条件中指定的Bean相同的值。 给定以下Java类 public class Bean {private Integer number;private String text;public Integer getNumber() {return number;}public void setNumber(Integer number) {this.number number;}public String getText() {return text;}public void setText(String text) {this.text text;} } 我们可以编写以下测试 Testpublic void test_samePropertyValuesAs() throws Exception {// GivenBean one new Bean();one.setText(text);one.setNumber(3);Bean two new Bean();two.setText(text);two.setNumber(3);// ThenassertThat(one, samePropertyValuesAs(two));} 3.1.46。 以。。开始 如果输入字符串以给定前缀开头的匹配项。 Testpublic void test_startsWith() throws Exception {// GivenString test Beginnings are important!;// ThenassertThat(test, startsWith(Beginning));} 3.1.47。 stringContainsInOrder 如果输入String包含给定Iterable中的子字符串则按从Iterable返回的顺序进行匹配的Matcher。 Testpublic void test_stringContainsInOrder() throws Exception {// GivenString test Rule number one: twos company, but threes a crowd!;// ThenassertThat(test, stringContainsInOrder(Arrays.asList(one, two, three)));} 3.1.48。 theInstance 当输入对象与指定值相同的实例时匹配的匹配器。 行为与“ sameInstance”相同 Test public void test_theInstance() throws Exception {// GivenObject one new Object();Object two one;// ThenassertThat(one, theInstance(two)); } 3.1.49。 typeCompatibleWith 当输入类型的对象可以分配给指定基本类型的引用时匹配的匹配器。 Testpublic void test_typeCompatibleWith() throws Exception {// GivenInteger integer 3;// ThenassertThat(integer.getClass(), typeCompatibleWith(Number.class));}简单匹配器结合其他匹配器 以下匹配器主要用于组合其他匹配器。 3.2.1。 所有的 当所有输入Matchers都匹配时匹配的Matcher的行为类似于逻辑AND。 可以使用单个Matchers或Iterable Matchers。 个人匹配器 Testpublic void test_allOf_individual() throws Exception {// GivenString test starting off well, gives content meaning, in the end;// ThenassertThat(test, allOf(startsWith(start), containsString(content), endsWith(end)));} 匹配器的迭代 Testpublic void test_allOf_iterable() throws Exception {// GivenString test Hello, world!;ListMatcher? super String matchers Arrays.asList(containsString(world), startsWith(Hello));// ThenassertThat(test, allOf(matchers));} 3.2.2。 任何 当任何输入匹配器匹配时匹配的匹配器其行为类似于逻辑或。 可以使用单个Matchers或Iterable Matchers。 个人匹配器 Testpublic void test_anyOf_individual() throws Exception {// GivenString test Some things are present, some things are not!;// ThenassertThat(test, anyOf(containsString(present), containsString(missing)));} 匹配器的迭代 Testpublic void test_anyOf_iterable() throws Exception {// GivenString test Hello, world!;ListMatcher? super String matchers Arrays.asList(containsString(Hello), containsString(Earth));// ThenassertThat(test, anyOf(matchers));} 3.2.3。 array 当输入数组的元素分别使用指定的Matchers依次进行匹配时匹配的Matcher。 匹配器的数量必须等于数组的大小。 Testpublic void test_array() throws Exception {// GivenString[] test {To be, or not to be, that is the question!};// ThenassertThat(test, array(startsWith(To), containsString(not), instanceOf(String.class)));} 3.2.4。 都 匹配器当与它的可组合匹配器.and结合使用时将在两个指定匹配器匹配时匹配。 Testpublic void test_both() throws Exception {// GivenString test Hello, world!;// ThenassertThat(test, both(startsWith(Hello)).and(endsWith(world!)));} 3.2.5。 要么 匹配器当与它的可组合匹配器.or结合使用时如果指定的匹配器匹配则匹配器。 Testpublic void test_either() throws Exception {// GivenString test Hello, world!;// ThenassertThat(test, either(startsWith(Hello)).or(endsWith(universe!)));} 3.2.6。 is 当输入匹配器匹配时匹配的匹配器只是为了方便起见使断言更像英语。 Testpublic void test_is_matcher() throws Exception {// GivenInteger test 5;// ThenassertThat(test, is(greaterThan(3)));} 也用作is(equalTo(...))的别名类似于not(...)和not(equalTo(...)) Testpublic void test_is_value() throws Exception {// GivenInteger test 5;// ThenassertThat(test, is(5));} 3.2.7。 被形容为 匹配器用于覆盖另一个匹配器的失败输出。 在需要自定义故障输出时使用。 参数是失败消息原始Matcher然后是将使用占位符012格式化为失败消息的所有值。 Testpublic void test_describedAs() throws Exception {// GivenInteger actual 7;Integer expected 10;// ThenassertThat(actual, describedAs(input %0, greaterThan(expected), expected));}4。结论 现在我们访问了Hamcrest中定义的所有Matchers并看到了每个匹配器的示例。 库中有很多非常有用且功能强大的Matchers尤其是当彼此结合使用时。 但是有时我们需要做的比现有的要多。 在下一个教程中我们将研究如何创建自己的自定义Matchers以扩展Hamcrest并使它更加有用 翻译自: https://www.javacodegeeks.com/2015/11/hamcrest-matchers-tutorial.html
http://www.huolong8.cn/news/251698/

相关文章:

  • 网站建设培训学校广州无锡网站制作的公司有哪些
  • 电影片头在线制作网站百度安装下载
  • 建设企业资质双网是哪两个网站深圳建设网站公司排名
  • 湛江市seo网站设计哪里好网站建设与推cctv-10
  • wordpress门户网站模板深圳建网站哪家公司好
  • 做网站 怎么选择公司推一把网络营销学院
  • 空间域名主机网站模板合肥网站建设优化
  • 黑龙江网站建设企业windows优化大师使用方法
  • 网站建设分金手指排名八广东上海专业网站建设公司
  • 做视频大赛推广的网站关键词都有哪些
  • 哪个建站平台较好做网站要付哪些钱
  • 直播网站建设方案网站开发语言有什么
  • 安康市城乡建设规划局 网站河南郑州网站推广优化外包
  • 免费网站开发软件有哪些基于php mysql的网站开发
  • 上杭县城乡规划建设局网站WordPress点击出现爱心
  • 完整网站开发视频教程望野诗
  • 网站自动屏蔽恶意点击在凡科上做的网站无法加载出来
  • 莆田网站建设费用家具营销策划方案
  • 电子商务网站建设定位设想自己做下载网站吗
  • 乐安网站建设外贸订单网站有哪些
  • 广东网站开发需要多少钱信息流投放平台
  • 秦皇岛做网站汉狮网络可以做ppt的网站有哪些
  • 博星卓越 网站开发方案有哪些网站可以做海报
  • 网站没完成可以备案么wordpress商城模板好用吗
  • 网站建设要备案吗班级网站设计与制作
  • 电子商务网站的建设内容app手机软件开发
  • 做网站建设的网站重启服务器 wordpress
  • 好文案网站太原百度搜索排名优化
  • 成都顶呱呱网站建设cpa推广之家
  • 豪柏大厦做网站的公司上海十大公司排名