一.Hamcrest是什么?
Hamcrest is a library of matchers, which can be combined in to create flexible expressions of intent in tests.
Hamcrest 是一个为了测试为目的,且能组合成灵活表达式的匹配器类库。
二.Hamcrest jar包
- hamcrest-core.jar -- This is the core API to be used by third-party framework providers. This includes the a foundation set of matcher implementations for common operations. This API is stable and will rarely change. You will need this library as a minimum.
- hamcrest-library.jar -- The ever-growing library of Matcher implementations. This will grow between releases.
- hamcrest-generator.jar -- A tool to allow many Matcher implementations to be combined into a single class so users don't have to remember many classes/packages to import. Generates code.
- hamcrest-integration.jar -- Provides integration between Hamcrest and other testing tools, including JUnit (3 and 4), TestNG, jMock and EasyMock.
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.*;
测试方法如下:
1.
2.
3.
public void testHamcrestMatchers() {
4.
// 核心匹配
5.
// allOf: 所有条件都必须满足,相当于&&
6.
assertThat("myname", allOf(startsWith("my"), containsString("name")));
7.
// anyOf: 其中一个满足就通过, 相当于||
8.
assertThat("myname", anyOf(startsWith("na"), containsString("name")));
9.
// both: &&
10.
assertThat("myname", both(containsString("my")).and(containsString("me")));
11.
// either: 两者之一
12.
assertThat("myname", either(containsString("my")).or(containsString("you")));
13.
// everyItem: 每个元素都需满足特定条件
14.
assertThat(Arrays.asList("my", "mine"), everyItem(startsWith("m")));
15.
// hasItem: 是否有这个元素
16.
assertThat(Arrays.asList("my", "mine"), hasItem("my"));
17.
// hasItems: 包含多个元素
18.
assertThat(Arrays.asList("my", "mine", "your"), hasItems("your", "my"));
19.
// is: is(equalTo(x))或is(instanceOf(clazz.class))的简写
20.
assertThat("myname", is("myname"));
21.
assertThat("mynmae", is(String.class));
22.
// anything(): 任何情况下,都匹配正确
23.
assertThat("myname", anything());
24.
// not: 否为真,相当于!
25.
assertThat("myname", is(not("you")));
26.
// nullValue(): 值为空
27.
String str = null;
28.
assertThat(str, is(nullValue()));
29.
// notNullValue(): 值不为空
30.
String str2 = "123";
31.
assertThat(str2, is(notNullValue()));
32.
33.
34.
// 字符串匹配
35.
// containsString:包含字符串
36.
assertThat("myname", containsString("na"));
37.
// stringContainsInOrder: 顺序包含,“my”必须在“me”前面
38.
assertThat("myname", stringContainsInOrder(Arrays.asList("my", "me")));
39.
// endsWith: 后缀
40.
assertThat("myname", endsWith("me"));
41.
// startsWith: 前缀
42.
assertThat("myname", startsWith("my"));
43.
// isEmptyString(): 空字符串
44.
assertThat("", isEmptyString());
45.
// equalTo: 值相等, Object.equals(Object)
46.
assertThat("myname", equalTo("myname"));
47.
assertThat(new String[] {"a", "b"}, equalTo(new String[] {"a", "b"}));
48.
// equalToIgnoringCase: 比较时,忽略大小写
49.
assertThat("myname", equalToIgnoringCase("MYNAME"));
50.
// equalToIgnoringWhiteSpace: 比较时, 首尾空格忽略, 比较时中间用单个空格
51.
assertThat(" my \t name ", equalToIgnoringWhiteSpace(" my name "));
52.
// isOneOf: 是否为其中之一
53.
assertThat("myname", isOneOf("myname", "yourname"));
54.
// isIn: 是否为其成员
55.
assertThat("myname", isIn(new String[]{"myname", "yourname"}));
56.
// toString() 返回值校验
57.
assertThat(333, hasToString(equalTo("333")));
58.
59.
60.
// 数值匹配
61.
// closeTo: [operand-error, operand+error], Double或BigDecimal类型
62.
assertThat(3.14, closeTo(3, 0.5));
63.
assertThat(new BigDecimal("3.14"), is(closeTo(new BigDecimal("3"), new BigDecimal("0.5"))));
64.
// comparesEqualTo: compareTo比较值
65.
assertThat(2, comparesEqualTo(2));
66.
// greaterThan: 大于
67.
assertThat(2, greaterThan(0));
68.
// greaterThanOrEqualTo: 大于等于
69.
assertThat(2, greaterThanOrEqualTo(2));
70.
// lessThan: 小于
71.
assertThat(0, lessThan(2));
72.
// lessThanOrEqualTo: 小于等于
73.
assertThat(0, lessThanOrEqualTo(0));
74.
75.
76.
77.
// 集合匹配
78.
// array: 数组长度相等且对应元素也相等
79.
assertThat(new Integer[]{1, 2, 3}, is(array(equalTo(1), equalTo(2), equalTo(3))));
80.
// hasItemInArray: 数组是否包含特定元素
81.
assertThat(new String[]{"my", "you"}, hasItemInArray(startsWith("y")));
82.
// arrayContainingInAnyOrder, 顺序无关,长度要一致
83.
assertThat(new String[]{"my", "you"}, arrayContainingInAnyOrder("you", "my"));
84.
// arrayContaining: 顺序,长度一致
85.
assertThat(new String[]{"my", "you"}, arrayContaining("my", "you"));
86.
// arrayWithSize: 数组长度
87.
assertThat(new String[]{"my", "you"}, arrayWithSize(2));
88.
// emptyArray: 空数组
89.
assertThat(new String[0], emptyArray());
90.
// hasSize: 集合大小
91.
assertThat(Arrays.asList("my", "you"), hasSize(equalTo(2)));
92.
// empty: 空集合
93.
assertThat(new ArrayList<String>(), is(empty()));
94.
// isIn: 是否为集合成员
95.
assertThat("myname", isIn(Arrays.asList("myname", "yourname")));
96.
// Map匹配
97.
Map<String, String> myMap = new HashMap<String, String>();
98.
myMap.put("name", "john");
99.
// hasEntry: key && value匹配
100.
assertThat(myMap, hasEntry("name", "john"));
101.
// hasKey: key匹配
102.
assertThat(myMap, hasKey(equalTo("name")));
103.
// hasValue: value匹配
104.
assertThat(myMap, hasValue(equalTo("john")));
105.
}
106.