时间:2021-05-20
一.Hamcrest是什么?
Hamcrest is a library of matchers, which can be combined in to create flexible expressions of intent in tests.
Hamcrest 是一个为了测试为目的,且能组合成灵活表达式的匹配器类库。
二.Hamcrest jar包
Alternatively, if you don't care:
hamcrest-all.jar -- Includes all of the above.
三.常用方法介绍
一般,在项目中导入hamcrest-core.jar, hamcrest-library.jar就能满足使用。
Eclipse新建一个JUnit test case; 测试类导入
import static org.hamcrest.Matchers.*;
测试方法如下:
@Testpublic void testHamcrestMatchers() { // 核心匹配 // allOf: 所有条件都必须满足,相当于&& assertThat("myname", allOf(startsWith("my"), containsString("name"))); // anyOf: 其中一个满足就通过, 相当于|| assertThat("myname", anyOf(startsWith("na"), containsString("name"))); // both: && assertThat("myname", both(containsString("my")).and(containsString("me"))); // either: 两者之一 assertThat("myname", either(containsString("my")).or(containsString("you"))); // everyItem: 每个元素都需满足特定条件 assertThat(Arrays.asList("my", "mine"), everyItem(startsWith("m"))); // hasItem: 是否有这个元素 assertThat(Arrays.asList("my", "mine"), hasItem("my")); // hasItems: 包含多个元素 assertThat(Arrays.asList("my", "mine", "your"), hasItems("your", "my")); // is: is(equalTo(x))或is(instanceOf(clazz.class))的简写 assertThat("myname", is("myname")); assertThat("mynmae", is(String.class)); // anything(): 任何情况下,都匹配正确 assertThat("myname", anything()); // not: 否为真,相当于! assertThat("myname", is(not("you"))); // nullValue(): 值为空 String str = null; assertThat(str, is(nullValue())); // notNullValue(): 值不为空 String str2 = "123"; assertThat(str2, is(notNullValue())); // 字符串匹配 // containsString:包含字符串 assertThat("myname", containsString("na")); // stringContainsInOrder: 顺序包含,“my”必须在“me”前面 assertThat("myname", stringContainsInOrder(Arrays.asList("my", "me"))); // endsWith: 后缀 assertThat("myname", endsWith("me")); // startsWith: 前缀 assertThat("myname", startsWith("my")); // isEmptyString(): 空字符串 assertThat("", isEmptyString()); // equalTo: 值相等, Object.equals(Object) assertThat("myname", equalTo("myname")); assertThat(new String[] { "a", "b" } , equalTo(new String[] { "a", "b" } )); // equalToIgnoringCase: 比较时,忽略大小写 assertThat("myname", equalToIgnoringCase("MYNAME")); // equalToIgnoringWhiteSpace: 比较时, 首尾空格忽略, 比较时中间用单个空格 assertThat(" my t name ", equalToIgnoringWhiteSpace(" my name ")); // isOneOf: 是否为其中之一 assertThat("myname", isOneOf("myname", "yourname")); // isIn: 是否为其成员 assertThat("myname", isIn(new String[]{ "myname", "yourname" } )); // toString() 返回值校验 assertThat(333, hasToString(equalTo("333"))); // 数值匹配 // closeTo: [operand-error, operand+error], Double或BigDecimal类型 assertThat(3.14, closeTo(3, 0.5)); assertThat(new BigDecimal("3.14"), is(closeTo(new BigDecimal("3"), new BigDecimal("0.5")))); // comparesEqualTo: compareTo比较值 assertThat(2, comparesEqualTo(2)); // greaterThan: 大于 assertThat(2, greaterThan(0)); // greaterThanOrEqualTo: 大于等于 assertThat(2, greaterThanOrEqualTo(2)); // lessThan: 小于 assertThat(0, lessThan(2)); // lessThanOrEqualTo: 小于等于 assertThat(0, lessThanOrEqualTo(0)); // 集合匹配 // array: 数组长度相等且对应元素也相等 assertThat(new Integer[]{ 1, 2, 3 } , is(array(equalTo(1), equalTo(2), equalTo(3)))); // hasItemInArray: 数组是否包含特定元素 assertThat(new String[]{ "my", "you" } , hasItemInArray(startsWith("y"))); // arrayContainingInAnyOrder, 顺序无关,长度要一致 assertThat(new String[]{ "my", "you" } , arrayContainingInAnyOrder("you", "my")); // arrayContaining: 顺序,长度一致 assertThat(new String[]{ "my", "you" } , arrayContaining("my", "you")); // arrayWithSize: 数组长度 assertThat(new String[]{ "my", "you" } , arrayWithSize(2)); // emptyArray: 空数组 assertThat(new String[0], emptyArray()); // hasSize: 集合大小 assertThat(Arrays.asList("my", "you"), hasSize(equalTo(2))); // empty: 空集合 assertThat(new ArrayList<String>(), is(empty())); // isIn: 是否为集合成员 assertThat("myname", isIn(Arrays.asList("myname", "yourname"))); // Map匹配 Map<String, String> myMap = new HashMap<String, String>(); myMap.put("name", "john"); // hasEntry: key && value匹配 assertThat(myMap, hasEntry("name", "john")); // hasKey: key匹配 assertThat(myMap, hasKey(equalTo("name"))); // hasValue: value匹配 assertThat(myMap, hasValue(equalTo("john")));}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
什么是Junit5?先看来个公式:JUnit5=JUnitPlatform+JUnitJupiter+JUnitVintage这看上去比Junit4复杂
当我写@Test注解时,一直报错后来百度是缺少junit4的包手动添加junit4包具体查看是在左上角file–>projectstructure点击进来以后选
javaList循环与Map循环的总结做了一下list和map的总结,没有什么技术含量,就全当复习了一下api。测试环境是在junit4下,如果没有自己写一个m
1.问题在Idea2017.3中,建立JUnit5的测试用例TestCase,同时建立JUnit4的测试套件,运行测试套件时提示出错“java.lang.Exc
问题:1、目前测试存在的问题2、Junit注意的细节3、Junit使用规范4、断言5、案例junit(单元测试框架)1、目前存在的问题1、目前的测试方法如果需要