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. HDU 2298 Toxophily 【二分+三分】

    一个人站在(0,0)处射箭,箭的速度为v,问是否能够射到(x,y)处,并求最小角度. 首先需要判断在满足X=x的情况下最大高度hmax是否能够达到y,根据物理公式可得 h=vy*t-0.5*g*t*t ...

  2. 介绍linux下利用编译bash设置root账号共用的权限审计设置

    在日常运维工作中,公司不同人员(一般是运维人员)共用root账号登录linux服务器进行维护管理,在不健全的账户权限审计制度下,一旦出现问题,就很难找出源头,甚是麻烦!在此,介绍下利用编译bash使不 ...

  3. python-基础案例

    范例一: 练习:元素分类 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值 ...

  4. Xcode视图调试

    视图调试 使用视图调试器检查您的视图层次结构,可以轻松地判断视图位置.大小以及实现问题. 在XCode中运行你的应用程序,在调试栏上点击“调试视图层次”按钮,进入视图调试器. XCode停止你的应用程 ...

  5. Linq To Entities 及其相关(进阶)

    上篇我们讲解了Linq To Entities的一些基本操作,这篇我们主要是讲解一些比较高级的东西:存储过程查询,SQL语句查询以及表达式树. 存储过程 首先来讲解存储过程查询. //Query a ...

  6. 2016科幻惊悚《第五波》HD720P.中英双字

    导演: J·布莱克森编剧: 苏珊娜·格兰特 / 阿齐瓦·高斯曼 / 杰夫·皮克纳 / 瑞克·杨西主演: 科洛·莫瑞兹 / 尼克·罗宾森 / 朗·里维斯顿 / 玛姬·丝弗 / 亚历克斯·罗伊 / 更多. ...

  7. linux实践——ELF分析

    一.ELF的部分结构定义 elf header(定义在/usr/include/elf.h)//64位的系统ELF文件头包括以下两个部分 #define EI_NIDENT (16) typedef ...

  8. 浩瀚先森(guohao1206.com)

    博客搬家啦,新博客地址:浩瀚先森 http://www.guohao1206.com

  9. 20145208 实验三 Java面向对象程序设计

    20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...

  10. 4.HBase In Action 第一章-HBase简介(1.1.2 数据创新)

    As we now know, many prominent internet companies, most notably Google, Amazon, Yahoo!, and Facebook ...