前言
Hamcrest是用于Java的单元测试的框架,它使用matcher
匹配器来进行断言。在Spring-boot-starter中集成了Hamcrest,无需而外引入。
核心API
- is
- 使用
is
匹配器使得程序更加易懂
String str1 = "text";
String str2 = " text ";
assertThat(str1, is(equalToIgnoringWhiteSpace(str2)));
- 用于简单数据类型的判断
String str1 = "text";
String str2 = "text";
assertThat(str1, is(str2));
- not
和is使用方式一样,只是含义相反。
- 使用
not
匹配器使得程序更加易懂
String str1 = "text";
String str2 = " text ";
assertThat(str1, not(equalToIgnoringWhiteSpace(str2)));
- 用于简单数据类型的判断
String str1 = "text";
String str2 = "text";
assertThat(str1, not(str2));
- containsString
是否包含子串
String str1 = "text123";
String str2 = "text";
assertThat(str1, containsString(str2));
- 以为某个字段开头/结尾
String str1 = "text123";
assertThat(str1, startsWith("text"));// 以某个字符开头
assertThat(str1, endsWith("text"));// 以某个字符开头
- 判断两个对象是否为同一个实体
Cat cat=new Cat();
assertThat(cat, sameInstance(cat));
- 类似OR的效果
String str = "calligraphy";
String start = "call";
String end = "foo";
assertThat(str, anyOf(startsWith(start), containsString(end)));
- 类似AND的效果
String str = "calligraphy";
String start = "call";
String end = "phy";
assertThat(str, allOf(startsWith(start), endsWith(end)));
匹配器
Number 匹配器
assertThat(1, greaterThan(0)); // 大于
assertThat(5, greaterThanOrEqualTo(5)); //大于等于
assertThat(-1, lessThan(0)); // 小于
assertThat(-1, lessThanOrEqualTo(5)); // 小于等于
Text 匹配器
- 匹配空串
String str = "";
assertThat(str, isEmptyString()); // 空字符串
assertThat(str, isEmptyOrNullString()); // 空字符串或者null
- 匹配字符串相等
String str1 = "text";
String str2 = " text ";
assertThat(str1, equalToIgnoringWhiteSpace(str2)); // 忽略左右空白
assertThat(a, equalToIgnoringCase(b)); //忽略大小写
Collections 匹配器
- 检查某个元素是否在集合中
List collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, hasItem("cd"));
assertThat(collection, not(hasItem("zz")));
assertThat(collection, hasItems("cd", "ab")); // 检查多个元素是否在集合中
- 检查所有元素
List collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, hasItems("ab","cd","ef"));
assertThat(collection, hasItems("cd", "ab","ef")); // 错误
assertThat(collection, hasItems("ab","cd")); // 错误
assertThat(collection, containsInAnyOrder("cd", "ab","ef")); //正确,不区分顺序
- 为空检查
- 集合为空检查
List collection = Lists.newArrayList("ab","cd","ef");
assertThat(collection, empty()); // false, 用于检查集合类型
- 数组为空检查
String[] array = new String[] { "ab" };
assertThat(array, emptyArray()); // false
- map为空检查
Map maps = Maps.newHashMap();
assertThat(maps, equalTo(Collections.EMPTY_MAP));
- Iterable为空检查
Iterable collection = Lists.newArrayList();
assertThat(collection, emptyIterable());
- 检查数目
- 检查集合数目
List list = Lists.newArrayList("ab", "cd", "ef");
assertThat(list, hasSize(3));
- 检查iterable数目
Iterable list = Lists.newArrayList("ab", "cd", "ef");
assertThat(list, iterableWithSize(3));
- 检查每项的条件
List list = Lists.newArrayList("ab", "cd", "ef");
assertThat(list, everyItem(equalTo("ab")));
Bean 匹配器
定义一个City的bean
@Data
@AllArgsConstructor
public class City {
String name;
String state;
}
- 测试是否存在某个属性
City city = new City("shenzhen", "CA");
assertThat(city, hasProperty("state"));
assertThat(city, hasProperty("state", equalTo("CA"))); // 判断是否存在某个属性,并且是否存在某个特性值
- 判断两个对象property值是否一样
City city = new City("San Francisco", "CA");
City city2 = new City("San Francisco", "CA");
assertThat(city, samePropertyValuesAs(city2));
refer
https://www.baeldung.com/java-junit-hamcrest-guide