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

南昌网站建设规划方案极速微网站建设cms

南昌网站建设规划方案,极速微网站建设cms,网站通用样式,网站核验点matchers依赖本文是我们名为“ 用Mockito测试 ”的学院课程的一部分。 在本课程中#xff0c;您将深入了解Mockito的魔力。 您将了解有关“模拟”#xff0c;“间谍”和“部分模拟”的信息#xff0c;以及它们相应的存根行为。 您还将看到使用测试双打和对象匹配器进行验证… matchers依赖 本文是我们名为“ 用Mockito测试 ”的学院课程的一部分。 在本课程中您将深入了解Mockito的魔力。 您将了解有关“模拟”“间谍”和“部分模拟”的信息以及它们相应的存根行为。 您还将看到使用测试双打和对象匹配器进行验证的过程。 最后讨论了使用Mockito的测试驱动开发TDD以了解该库如何适合TDD的概念。 在这里查看 在本教程中我们将使用Hamcrest API创建我们自己的自定义匹配器以扩展Hamcrest提供的“开箱即用”功能。 目录 1.为什么要使用自定义匹配器 2.匹配器的解剖 3.现有课程的自定义匹配器 3.1。 甚至 3.2。 divisibleBy整数除数 4.针对您自己的班级的自定义匹配器 4.1。 我们的模型一棵树 4.2。 叶 4.3。 根 4.4。 DescendantOf节点节点 4.5。 ancestorOf节点节点 4.6。 siblingOf节点节点 5.结论 1.为什么要使用自定义匹配器 有时我们会遇到Hamcrest Matchers库的限制。 我们需要在标准类例如字符串整数或列表上具有新的匹配器功能或者需要创建与已创建的高度自定义类匹配的匹配器。 在本教程中我们将使用Hamcrest提供的工具针对这两种情况创建匹配器。 2.匹配器的解剖 为了创建自定义匹配器我们将扩展内置的Abstract类TypeSafeDiagnosingMatcher 。 如果您在IDE中将此类扩展为Integer类型您将看到该类的两个抽象方法 public class BlankMatcher extends TypeSafeDiagnosingMatcherInteger {Overrideprotected boolean matchesSafely(Integer integer, Description description) {return false;}Overridepublic void describeTo(Description description) {} } 第一个方法matchesSafely()是Matcher的作用所在这是Hamcrest在要使用Matcher测试值时执行的方法。 如果是这种情况它还负责报告Matcher为何不匹配。 不匹配描述是Hamcrest在失败匹配器输出的“但是”部分之后使用的描述因此不匹配描述应相应地设置格式。 第二种方法describeTo()用于生成匹配器正在检查的内容的描述。 Hamcrest在失败的匹配器的输出的“ Expected”部分之后使用此描述因此该描述应相应设置格式。 在后台此Matcher将检查输入值是否不为null且类型正确因此在执行命中我们的matchesSafely方法时我们保证具有正确类型的值以进行匹配。 Hamcrest为我们完成了繁重的工作因此这两种方法都是实现自己的Hamcrest Matchers所需要的。 在接下来的示例中我们还将添加一些语法糖以简化创建和使用自定义匹配器的过程。 3.现有课程的自定义匹配器 在本节中我们将创建许多可用于现有类型的自定义匹配器。 甚至 让我们从创建匹配器开始以确定输入数字是否为偶数。 和以前一样我们将为整数扩展TypeSafeDiagnosingMatcher 。 public class IsEven extends TypeSafeDiagnosingMatcherInteger {Overrideprotected boolean matchesSafely(Integer integer, Description description) {return false;}Overridepublic void describeTo(Description description) {} } 然后我们将实现describeTo()方法以提供对匹配器的期望描述 Override public void describeTo(Description description) {description.appendText(An Even number); } 我们可以使用description参数来创建Matcher的描述。 Description类提供了许多用于格式化输入的辅助方法在这种情况下我们使用appendText()方法简单地添加一些文本。 接下来让我们使用传递到matchesSafely方法的description参数来创建错误消息。 请记住只有在故障情况下才能看到此输出。 我们将在Description类中使用几种方法来格式化消息 Override protected boolean matchesSafely(Integer integer, Description description) {description.appendText(was ).appendValue(integer).appendText(, which is an Odd number);return false; } 接下来我们将对数字进行实际检查以查看其是否为偶数 Override protected boolean matchesSafely(Integer integer, Description description) {description.appendText(was ).appendValue(integer).appendText(, which is an Odd number);return integer % 2 0; } 最后我们将向类添加静态工厂方法以使其易于在测试中使用 public static IsEven isEven() {return new IsEven(); } 总而言之我们有以下Matcher类 public class IsEven extends TypeSafeDiagnosingMatcherInteger {Overrideprotected boolean matchesSafely(Integer integer, Description description) {description.appendText(was ).appendValue(integer).appendText(, which is an Odd number);return integer % 2 0;}Overridepublic void describeTo(Description description) {description.appendText(An Even number);}public static IsEven isEven() {return new IsEven();} } 现在我们可以使用新的匹配器编写一些测试。 我们创建一个名为IsEvenTest的新Test类然后导入新的工厂方法 import static com.javacodegeeks.hughwphamill.mockito.hamcrest.matchers.IsEven.isEven;public class IsEvenTest {} 接下来我们将编写一种测试方法来测试匹配器评估为true的肯定情况。 Test public void should_pass_for_even_number() throws Exception {// GivenInteger test 4;// ThenassertThat(test, isEven()); } 还有一种显示匹配器无法匹配的输出的方法。 Test public void should_fail_for_odd_number() throws Exception {// GivenInteger test 5;// ThenassertThat(test, isEven()); } 这将生成以下输出 java.lang.AssertionError: Expected: An Even numberbut: was 5, which is an Odd number 通常在为匹配器编写真实测试时我们希望通过测试以确保逻辑是正确的毕竟我们不希望在测试失败的项目上工作 如果我们想这样做我们可以将断言更改为以下内容 assertThat(test, not(isEven())); 但是在开发过程中编写失败的测试以手动检查匹配器的输出可能会很有用。 divisibleBy整数除数 现在我们已经看到了如何创建一个自定义Matcher来测试Integer的固有属性。 是奇数还是偶数 但是我们在hamcrest Matcher库中看到许多Matchers会根据输入值进行测试。 现在我们将自己创建一个Matcher。 想象一下我们想定期测试数字以发现它们是否可以被另一个数字整除。 我们可以编写一个自定义Matcher为我们完成此任务。 首先像上一个示例一样创建一个无参匹配器并将除数硬编码为3。 public class DivisibleBy extends TypeSafeDiagnosingMatcherInteger {Overrideprotected boolean matchesSafely(Integer integer, Description description) {int remainder integer % 3; // Hardcoded to 3 for now!description.appendText(was ).appendValue(integer).appendText( which left a remainder of ).appendValue(remainder);return remainder 0;}Overridepublic void describeTo(Description description) {description.appendText(A number divisible by 3); // Hardcoded to 3 for now!}public static DivisibleBy divisibleBy() {return new DivisibleBy();} } 我们的Matcher看起来很像我们的IsEven() Matcher但是不匹配的描述稍微复杂一些。 我们如何从硬编码值更改为输入值 没什么花招实际上就像添加一个私有成员变量并将其设置在构造函数中然后将值通过factory方法传递一样容易。 现在让我们看一下完整的Matcher public class DivisibleBy extends TypeSafeDiagnosingMatcherInteger {private final Integer divisor;public DivisibleBy(Integer divisor) {this.divisor divisor;}Overrideprotected boolean matchesSafely(Integer integer, Description description) {int remainder integer % 3; // Hardcoded to 3 for now!description.appendText(was ).appendValue(integer).appendText( which left a remainder of ).appendValue(remainder);return remainder 0;}Overridepublic void describeTo(Description description) {description.appendText(A number divisible by 3); // Hardcoded to 3 for now!}public static DivisibleBy divisibleBy(Integer divisor) {return new DivisibleBy(divisor);} } 再次让我们创建一些测试来练习我们的新Matcher public class DivisibleByTest {Testpublic void should_pass_for_true_divisor() throws Exception {// GivenInteger test 15;// ThenassertThat(test, is(divisibleBy(5)));}Testpublic void should_fail_for_non_divisor() throws Exception {// GivenInteger test 17;// ThenassertThat(test, is(divisibleBy(3)));} } 测试失败的输出是 java.lang.AssertionError: Expected: is A number divisible by 3but: was 17 which left a remainder of 2 现在我们已经看到了如何为现有类创建自定义匹配器这些类可以测试内部属性或接受测试值。 接下来我们将为自己编写的类创建自定义匹配器。 4.针对您自己的班级的自定义匹配器 当我们使用自己创建的类进行工作时自定义Hamcrest匹配器是测试库中的强大工具。 现在我们将创建一个域模型并编写一些自定义匹配器以使用该模型。 我们的模型一棵树 编程中常见的数据结构是由许多节点组成的树。 这些节点可能只有一个父节点或一个或多个子节点。 没有父节点的节点称为根。 没有子节点的节点称为叶子。 如果可以通过X的父级从X到Y跟踪路径则将节点X视为另一个节点Y的后代。如果Y是X的后代则将节点X视为另一个节点Y的祖先。如果X和Y都共享父节点则视为另一个节点Y的兄弟。 我们将使用Node存储单个int值。 我们的模型将有一个名为Node的类如下所示 /** * Node class for building trees ** Uses instance equality. */ public class Node {private final int value;private Node parent;private final SetNode children;/*** Create a new Node with the input value*/public Node(int value) {this.value value;children new HashSet();}/*** return The value of this Node*/public int value() {return value;}/*** return The parent of this Node*/public Node parent() {return parent;}/*** return A copy of the Set of children of this Node*/public SetNode children() {return new HashSet(children);}/*** Add a child to this Node** return this Node*/public Node add(Node child) {if (child ! null) {children.add(child);child.parent this;}return this;}/*** Remove a child from this Node** return this Node*/public Node remove(Node child) {if (child ! null children.contains(child)) {children.remove(child);child.parent null;}return this;}public String toString() {StringBuilder builder new StringBuilder();builder.append(Node{).append(value).append(value).append(,).append(parent).append(parent ! null ?parent.value : null).append(,).append(children).append([).append(children.stream().map(n - Integer.toString(n.value)).collect(Collectors.joining(,))).append(]});return builder.toString();} } 请注意对于这个简单的示例我们的类不是线程安全的。 该模型使我们能够构建一个节点树从根节点开始并添加子节点。 我们可以在下面的小型应用程序类中看到这一点 public class App {public static void main(String... args) {Node root createTree();printNode(root);}private static Node createTree() {/*1/ \2 3/ \ / \4 5 6 7/ \ |8 9 10*/Node root new Node(1);root.add(new Node(2).add(new Node(4).add(new Node(8)).add(new Node(9))).add(new Node(5).add(new Node(10)))).add(new Node(3).add(new Node(6)).add(new Node(7)));return root;}private static void printNode(Node node) {System.out.println(node);for (Node child : node.children()) {printNode(child);}} } 这将产生以下输出 Node{value1,parentnull,children[3,2]} Node{value3,parent1,children[7,6]} Node{value7,parent3,children[]} Node{value6,parent3,children[]} Node{value2,parent1,children[5,4]} Node{value5,parent2,children[10]} Node{value10,parent5,children[]} Node{value4,parent2,children[8,9]} Node{value8,parent4,children[]} Node{value9,parent4,children[]} 现在我们已经定义了模型并知道如何使用它我们可以开始针对它创建一些Matchers。 我们将在类中使用Node测试治具以便针对一致的模型进行测试该模型将与示例应用程序中定义的树结构相同。 夹具在这里定义 public class NodeTestFixture {static Node one new Node(1);static Node two new Node(2);static Node three new Node(3);static Node four new Node(4);static Node five new Node(5);static Node six new Node(6);static Node seven new Node(7);static Node eight new Node(8);static Node nine new Node(9);static Node ten new Node(10);static {one.add(two);one.add(three);two.add(four);two.add(five);three.add(six);three.add(seven);four.add(eight);four.add(nine);five.add(ten);} }叶 我们将创建的第一个Matcher将检查输入节点是否为叶节点。 它将通过检查输入节点是否有任何子节点来完成此操作如果有则子节点不是叶节点。 public class IsLeaf extends TypeSafeDiagnosingMatcherNode {Overrideprotected boolean matchesSafely(Node node, Description mismatchDescription) {if (!node.children().isEmpty()) {mismatchDescription.appendText(a node with ).appendValue(node.children().size()).appendText( children);return false;}return true;}Overridepublic void describeTo(Description description) {description.appendText(a leaf node with no children);}public static IsLeaf leaf() {return new IsLeaf();} } 测试类别 public class IsLeafTest extends NodeTestFixture {Testpublic void should_pass_for_leaf_node() throws Exception {// GivenNode node NodeTestFixture.seven;// ThenassertThat(node, is(leaf()));}Testpublic void should_fail_for_non_leaf_node() throws Exception {// GivenNode node NodeTestFixture.four;// ThenassertThat(node, is(not(leaf())));} }根 现在我们将创建一个匹配器以检查节点是否为根节点。 我们将通过检查父节点的存在来做到这一点。 public class IsRoot extends TypeSafeDiagnosingMatcherNode {Overrideprotected boolean matchesSafely(Node node, Description mismatchDescription) {if (node.parent() ! null) {mismatchDescription.appendText(a node with parent ).appendValue(node.parent());return false;}return true;}Overridepublic void describeTo(Description description) {description.appendText(a root node with no parent);}public static IsRoot root() {return new IsRoot();} } 测试类别 public class IsRootTest {Testpublic void should_pass_for_root_node() throws Exception {// GivenNode node NodeTestFixture.one;// ThenassertThat(node, is(root()));}Testpublic void should_fail_for_non_root_node() throws Exception {// GivenNode node NodeTestFixture.five;// ThenassertThat(node, is(not(root())));} }DescendantOf节点节点 接下来是带有输入的匹配器我们将检查给定Node是否为输入Node的后代。 我们将向上移动父母看看是否在根之前到达了测试节点。 public class IsDescendant extends TypeSafeDiagnosingMatcherNode {private final Node ancestor;public IsDescendant(Node ancestor) {this.ancestor ancestor;}Overrideprotected boolean matchesSafely(Node node, Description description) {while (node.parent() ! null) {if (node.parent().equals(ancestor)) {return true;}node node.parent();}description.appendText(a Node which was not a descendant of ).appendValue(ancestor);return false;}Overridepublic void describeTo(Description description) {description.appendText(a descendant Node of ).appendValue(ancestor);}public static IsDescendant descendantOf(Node ancestor) {return new IsDescendant(ancestor);} } 测试类别 public class IsDescendantTest {Testpublic void should_pass_for_descendant_node() throws Exception {// GivenNode node NodeTestFixture.nine;Node ancestor NodeTestFixture.two;// ThenassertThat(node, is(descendantOf(ancestor)));}Testpublic void should_fail_for_non_descendant_node() throws Exception {// GivenNode node NodeTestFixture.ten;Node ancestor NodeTestFixture.three;// ThenassertThat(node, is(not(descendantOf(ancestor))));} }ancestorOf节点节点 下一步将检查给定的节点是否是输入节点的祖先。 此操作实际上是descendantOf()的相反操作因此我们将上移输入节点的父级而不是测试节点。 public class IsAncestor extends TypeSafeDiagnosingMatcherNode {private final Node descendant;public IsAncestor(Node descendant) {this.descendant descendant;}Overrideprotected boolean matchesSafely(Node node, Description description) {Node descendantCopy descendant;while (descendantCopy.parent() ! null) {if (descendantCopy.parent().equals(node)) {return true;}descendantCopy descendantCopy.parent();}description.appendText(a Node which was not an ancestor of ).appendValue(descendant);return false;}Overridepublic void describeTo(Description description) {description.appendText(an ancestor Node of ).appendValue(descendant);}public static IsAncestor ancestorOf(Node descendant) {return new IsAncestor(descendant);} } 测试类别 public class IsAncestorTest {Testpublic void should_pass_for_ancestor_node() throws Exception {// GivenNode node NodeTestFixture.two;Node descendant NodeTestFixture.ten;// ThenassertThat(node, is(ancestorOf(descendant)));}Testpublic void should_fail_for_non_ancestor_node() throws Exception {// GivenNode node NodeTestFixture.three;Node descendant NodeTestFixture.eight;// ThenassertThat(node, is(not(ancestorOf(descendant))));} }siblingOf节点节点 最后我们将创建一个Matcher来检查输入节点是否是另一个节点的同级节点。 我们将检查他们是否共享父母。 此外当用户尝试测试根节点的同级节点时我们将进行一些检查并提供一些输出。 public class IsSibling extends TypeSafeDiagnosingMatcherNode {private final Node sibling;public IsSibling(Node sibling) {this.sibling sibling;}Overrideprotected boolean matchesSafely(Node node, Description description) {if (sibling.parent() null) {description.appendText(input root node cannot be tested for siblings);return false;}if (node.parent() ! null node.parent().equals(sibling.parent())) {return true;}if (node.parent() null) {description.appendText(a root node with no siblings);}else {description.appendText(a node with parent ).appendValue(node.parent());}return false;}Overridepublic void describeTo(Description description) {if (sibling.parent() null) {description.appendText(a sibling of a root node);} else {description.appendText(a node with parent ).appendValue(sibling.parent());}}public static IsSibling siblingOf(Node sibling) {return new IsSibling(sibling);} } 测试类别 public class IsSiblingTest {Testpublic void should_pass_for_sibling_node() throws Exception {// GivenNode a NodeTestFixture.four;Node b NodeTestFixture.five;// ThenassertThat(a, is(siblingOf(b)));}Testpublic void should_fail_for_testing_root_node() throws Exception {// GivenNode a NodeTestFixture.one;Node b NodeTestFixture.six;// ThenassertThat(a, is(not(siblingOf(b))));}Testpublic void should_fail_for_input_root_node() throws Exception {// GivenNode a NodeTestFixture.five;Node b NodeTestFixture.one;// ThenassertThat(a, is(not(siblingOf(b))));}Testpublic void should_fail_for_non_sibling_node() throws Exception {// GivenNode a NodeTestFixture.five;Node b NodeTestFixture.six;// ThenassertThat(a, is(not(siblingOf(b))));} }5.结论 现在我们已经看到了如何创建Custom Hamcrest Matchers来测试既有的标准Java类和我们自己的类。 在下一个教程中我们将把到目前为止所学到的所有知识放在一起因为我们了解了将模拟和测试放在首位的软件开发技术。 测试驱动开发。 翻译自: https://www.javacodegeeks.com/2015/11/custom-hamcrest-matchers.htmlmatchers依赖
http://www.pierceye.com/news/828567/

相关文章:

  • 律师事务所手机网站网站开发过程模型
  • 建筑培训网站系统开发包括什么
  • 出售家教网站模板广告设计公司管理
  • 松原网站推广wordpress主题更新了
  • wordpress 手机端模板百度seo标题优化软件
  • 货架网站开发特卖网站设计
  • 网站首页设计图片简约简单的明星个人网站建设论文
  • 织梦程序来搭建网站vip视频解析网站建设
  • 网站的管理上海创新网站建设
  • 企业对比网站西安做网站公司怎么样
  • 网站开发好做还是平面好做商务网页设计与制作是什么
  • 个人业务网站带后台凡科网站建设分类模块怎么弄
  • 在百度做网站需要什么资料appstore正版下载
  • wordpress怎么做404页面合肥seo软件
  • 建设网站挂广告赚钱免费个人网站源码
  • 网站ico图标动漫设计学什么内容
  • fireworks做网站定制做网站费用
  • 建设门户网站所需优秀营销网站设计
  • 行业网站建设教程办一家建筑公司流程
  • 网站空间文件夹中企动力主要是做什么的
  • 亚马逊做qa的网站wordpress theme是什么
  • 网站开发的经费预算php网站超市源码下载
  • 深圳建设高端网站asp.net 获取网站的绝对路径
  • 做的网站没流量吗前端页面设计
  • 门户网站的优点在环评备案网站上做登记后会怎么样
  • 网站的内容规划怎么写网站做外链的具体步骤
  • 百度网站排名规则小程序网站建设y021
  • 中国建设银行国际互联网站国内排名前五的电商
  • 怎么查网站的空间商四川建设工程招标网
  • 网站建设比较好公司朝阳区互联网公司排名