Hamcrest
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的更多相关文章
- [转]JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
原文引自: http://blog.csdn.net/castle07/article/details/8553704 今天尝试使用JUnit,下载了最新的JUnit版本,是4.11,结果尝试使用发现 ...
- 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
下载了最新的JUnit版本,是4.12,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误, 上网查 ...
- 软件测试作业3--Junit、hamcrest、eclemmat的安装和使用
1. how to install junit, hamcrest and eclemma? 首先下载下来Junit和Hamcrest的jar包,然后新建项目的时候将这两个jar包导入到工程里面就 ...
- junit4新框架hamcrest
Hamcrest是一个书写匹配器对象时允许直接定义匹配规则的框架.有大量的匹配器是侵入式的,例如UI验证或者数据过滤,但是匹配对象在书写灵活的测试是最常用.本教程将告诉你如何使用Hamcrest进行单 ...
- Junit,hamcrest和Eclemma安装及使用
Junit和hamcrest下载及安装 下载地址 Junit http://download.csdn.net/detail/luzle/6915487 Hamcrest http://d ...
- Junit 断言 assertThat Hamcrest匹配器
junit断言总结本文参考了http://blog.csdn.net/wangpeng047/article/details/9628449一 junit断言1.JUnit框架用一组assert方法封 ...
- 学习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 ...
- JUnit-4.11使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
今天尝试使用JUnit,下载了最新的JUnit版本,是4.11,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribi ...
- org/hamcrest/SelfDescribing
今天在Idea下使用JUtil的时候,运行@Test,报错:org/hamcrest/SelfDescribing 解决办法: (1)换成junit-4.8.jar (2)junit-4.11.jar ...
随机推荐
- NSURLSession学习笔记
NSURLSession学习笔记(一)简介 一.URL Session的基本概念 1.三种工作模式: 默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁 ...
- swift为UIView添加extension扩展frame
添加swift file:UIView+Extension import UIKit extension UIView { // x var x : CGFloat { get { return fr ...
- 如何将list转为json?
- MyEclipse无法启动调试:Cannot connect to VM
MyEclipse无法启动调试:Cannot connect to VM 问题描述:Eclipse普通的Run模式没有问题,Debug模式却启动不了.换了Eclipse,MyEclipse,JDK都不 ...
- 使用lftp传输文件的shell脚本
学习参考用,需要服务器上安装lftp. #!/bin/bash #date filepath=/usr/hadoop/bigdata/filterurl filtercount=$(ls $filep ...
- delphi数组作为参数传值
在函数中如果数组的个数不定,可以使用开放数组参数 实参可以接受静态数组和动态数组 procedure p1(a:array of Byte); begin ShowMessage( IntToHex( ...
- 移动Web 开发中的一些前端知识收集汇总
在开发DeveMobile 与EaseMobile 主题 的时候积累了一些移动Web 开发的前端知识,本着记录总结的目的,特写这篇文章备忘一下. 要说移动Web 开发与传统的PC 端开发,感觉也没什么 ...
- Discuz! X的CSS加载机制
首先,每个页面都会加载以下两个css,data/cache/style_1_common.css和data/cache/style_1_forum_index.css.先讲讲这两个文件名的命名规则:第 ...
- StringBuffer类 和 StringBuilder类
上一篇中讲解了String类的用法.那么String有什么特点呢? 字符串特点:字符串是常量,其值在创建后就不能被修改.字符串的内容一旦发生变化,就会创建一个新的对象. 代码验证字符串特点: publ ...
- IIS增加并发数
网站出现这样的错误信息:Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@appConcurrentRequ ...