Hamcrest使用

介绍

Hamcrest是一个开源的库,07年被引入到Junit4。assertThat()是其中最常用的一个方法,位于org.junit.Assert包中, 方法签名如下:static <T> void assertThat(T actual, org.hamcrest.Matcher<T> matcher)

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.edgibbs.junit.example;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class HamcrestExamples {

@Test
public void allOfExampleShowsAllMatchersMustAllBeTrue() throws Exception {
assertThat("Hello", is(allOf(notNullValue(), instanceOf(String.class), equalTo("Hello"))));
}

@Test
public void allOfExampleShowsFailingIfOneMatcherDoesNotMatch() throws Exception {
assertThat("Hello", is(not(allOf(notNullValue(), instanceOf(Integer.class)))));
}

@Test
public void anyExampleChecksThatClassIsOfSameType() throws Exception {
assertThat("Hello", is(any(String.class)));
}

@Test
public void anyExampleShowsStringIsAlsoAnObject() throws Exception {
assertThat("Hello", is(any(Object.class)));
}

@Test
public void anyOfExampleReturnsTrueIfOneMatches() throws Exception {
assertThat("Hello", is(anyOf(nullValue(), instanceOf(String.class), equalTo("Goodbye"))));
}

@Test
public void anyOfExampleFailingIfAllMatchersAreFalse() throws Exception {
assertThat("Hello", is(not(anyOf(nullValue(), instanceOf(Integer.class), equalTo("Goodbye")))));
}

@Test
public void anythingExampleAlwaysReturnsTrue() throws Exception {
assertThat("Hello", is(anything()));
}

// Feels very esoteric and not for typical usage used to override the description
@Test
public void describedAsExample() throws Exception {
Matcher< ?> matcher = describedAs("My Description", anything());
Description description = new StringDescription().appendDescriptionOf(matcher);
assertThat("My Description", is(description.toString()));
}

@Test
public void equalToExampleAddingTwoPlusTwo() throws Exception {
assertThat(2 + 2, is(equalTo(4)));
}

@Test
public void instanceOfExampleForString() throws Exception {
assertThat("Hello", is(instanceOf(String.class)));
}

@Test
public void isExampleShortCutForIsInstanceOfClass() throws Exception {
assertThat("Hello", is(String.class));
assertThat("Hello", instanceOf(String.class));
}

@Test
public void isExampleShortCutAsJustSyntacticSugarUsedThreeTimes() throws Exception {
assertThat("Hello", is(is(is(notNullValue()))));
}

@Test
public void isExampleShortCutForIsEqualTo() throws Exception {
assertThat("Hello", is("Hello"));
assertThat("Hello", equalTo("Hello"));
}

@Test
public void notExampleJustInvertsExpression() throws Exception {
assertThat("Hello", is(not(instanceOf(Integer.class))));
}

@Test
public void notNullValueExampleForString() throws Exception {
assertThat("Hello", is(notNullValue()));
}

@Test
public void notNullValueExampleForAClass() throws Exception {
assertThat("Hello", is(notNullValue(Object.class)));
}

@Test
public void nullValueExampleWithANull() throws Exception {
assertThat(null, is(nullValue()));
}

@Test
public void nullValueExampleWithANullType() throws Exception {
Integer nothing = null;
assertThat(nothing, is(nullValue(Integer.class)));
}

@Test
public void sameInstanceExample() throws Exception {
Object object = new Object();
Object sameObject = object;
assertThat(object, is(sameInstance(sameObject)));
}

}