Hamcrest比起JUnit的assert系列方法来,有更好的可读性,它按照参数从左到右的符合自然的顺序来展示,如actual is(notNullValue()),是对测试断言的改进。同时不会被哪个参数是actual,哪个是expect而混淆。除此之外,Hamcrest提供了更丰富的比较方法,不仅包括基本类型,也包括对象判断、集合/数组判断等一系列方法,同时还有allOf/anyOf这样的组合判断,甚至有判断xml中是否存在相应xpath的判断。

所有的Hamcrest支持的assert全部在org.hamcrest.Matchers类下的静态方法。下面是几个使用例子:

public class CoreShowcase {
@Test
public void test(){
assertThat(true,is(true));
assertThat(false,is(false)); assertThat("2",is(equalTo("2")));
assertThat(2,is(equalTo(2)));
assertThat("aBcD",is(equalToIgnoringCase("abcd")));
assertThat("",isEmptyOrNullString()); assertThat(null,is(nullValue()));
assertThat(2,is(notNullValue()));
assertThat(2,greaterThan(1));
assertThat(2.01,closeTo(2,0.01)); assertThat("xyz",is(anything()));
}
}

  

public class ObjectShowcase {
@Test
public void test(){
Person my=new Person("341024","ZhangKe",32); assertThat(my,instanceOf(Person.class));
assertThat(my,not(sameInstance(new Person("341024"))));
assertThat(my,hasToString(equalTo("341024")));
assertThat(my,hasProperty("name"));
} class Person{
private String idCard;
private String name;
private int age; public Person(String idCard) {
this.idCard = idCard;
} public Person(String idCard, String name, int age) {
this.idCard = idCard;
this.name = name;
this.age = age;
} public String getIdCard() {
return idCard;
} public String getName() {
return name;
} public int getAge() {
return age;
} @Override
public String toString() {
return idCard;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; if (!idCard.equals(person.idCard)) return false; return true;
} @Override
public int hashCode() {
return idCard.hashCode();
}
}
}

  

public class CombineShowcase {
@Test
public void test(){
//in both/either must have the same type
assertThat("result", both(containsString("r")).and(containsString("u")));
assertThat("result",either(startsWith("x")).or(containsString("r"))); assertThat("result",allOf(notNullValue(),containsString("s")));
assertThat("result",anyOf(notNullValue(),isEmptyString()));
}
}

  

Hamcrest的更多相关文章

  1. [转]JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误

    原文引自: http://blog.csdn.net/castle07/article/details/8553704 今天尝试使用JUnit,下载了最新的JUnit版本,是4.11,结果尝试使用发现 ...

  2. 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误

    下载了最新的JUnit版本,是4.12,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误, 上网查 ...

  3. 软件测试作业3--Junit、hamcrest、eclemmat的安装和使用

    1.   how to install junit, hamcrest and eclemma? 首先下载下来Junit和Hamcrest的jar包,然后新建项目的时候将这两个jar包导入到工程里面就 ...

  4. junit4新框架hamcrest

    Hamcrest是一个书写匹配器对象时允许直接定义匹配规则的框架.有大量的匹配器是侵入式的,例如UI验证或者数据过滤,但是匹配对象在书写灵活的测试是最常用.本教程将告诉你如何使用Hamcrest进行单 ...

  5. Junit,hamcrest和Eclemma安装及使用

    Junit和hamcrest下载及安装 下载地址 Junit      http://download.csdn.net/detail/luzle/6915487 Hamcrest  http://d ...

  6. Junit 断言 assertThat Hamcrest匹配器

    junit断言总结本文参考了http://blog.csdn.net/wangpeng047/article/details/9628449一 junit断言1.JUnit框架用一组assert方法封 ...

  7. 学习hamcrest和mockito时的总结和demo

    UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...

  8. JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误

    今天尝试使用JUnit,下载了最新的JUnit版本,是4.11,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribi ...

  9. org/hamcrest/SelfDescribing

    今天在Idea下使用JUtil的时候,运行@Test,报错:org/hamcrest/SelfDescribing 解决办法: (1)换成junit-4.8.jar (2)junit-4.11.jar ...

随机推荐

  1. eclipse luna maven失效的原因

    昨天发现单位里的eclipse中的maven直接不显示了,不能在 Windows-Preference 中显示maven 也不能新建maven工程,也不能maven-update,连STS(Sprin ...

  2. 2D Tookit (一) 精灵切割

    Sprite Dicing 精灵切割 图一:原图 Diced 设置   Diced[切割]对比图 文档 http://www.2dtoolkit.com/docs/latest/advanced/sp ...

  3. java 21 - 9 复制图片的4种方式

    需求:复制图片 分析: 因为图片我们用记事本打开后无法读懂,所以用字节流 并且字节流复制有4种方式,所以我们尝试4种方式. 推荐第四种:缓冲字节流一次读取一个字节数组 首先写main方法: publi ...

  4. Android中static和final用法小结

    Java关键字static.final使用小结 static  1. static变量     按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或类变量:另一种是 ...

  5. Redis word bak

    @font-face { font-family: "Arial"; }@font-face { font-family: "Courier New"; }@f ...

  6. kprobe原理解析(二)

    上一篇文章和大家简要说明了下kprobe到底应该怎样用,那么现在我们就揭开kprobe神秘的面纱,刨根问底,一睹kprobe的庐山真面目. kprobe的工作过程大致如下: 1)注册kprobe.注册 ...

  7. 使用DataAnnotations实现数据验证

    https://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx http://www.cnblo ...

  8. Managing the Lifecycle of a Service

    service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径: A started service 被开启的service通过其他组件调用 startService()被创建. 这种 ...

  9. jquery 现实多状态控件 (status & power(2,0)) = power(2,0)

    数据库表设计的时候,会有很些多状态的需求,比如招聘职位需要同时发布到武汉,广州,上海 实现方法有很多种,我选择了在职位表中建一个 int 型字段保存多种状态,这个涉及到一些算法,我要查询武汉和广州的职 ...

  10. C语言 文件操作8--fputs()和fgets()

    //fputs()和fgets() #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> # ...