1.JUnit4.*引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,這些匹配符更接近天然語言,可讀性高,更加靈活;
2.使用全新的斷言語法:assertThat,結合Hamcest提供的匹配符,只用這一個方法,就能夠實現全部的測試;
3.assertThat語法以下:
assertThat(T actual, Matcher<T> matcher);
assertThat(String reason, T actual, Matcher<T> matcher);
其中actual爲須要測試的變量,matcher爲使用Hamcrest的匹配符來表達變量actual指望值的聲明;
4.注意事項:
a.必須導入JUnit4.4以後的版本才能使用assertThat方法;
b.不須要繼承TestCase類,可是須要測試方法前必須加「@Test」。java
實例:
T.java:
框架
01.
package
cn.edu.ahau.mgc.junit4;
測試
02.
spa
03.
import
java.util.List;
.net
04.
import
java.util.ArrayList;
rest
05.
import
java.util.HashMap;
code
06.
import
java.util.Map;
繼承
07.
ci
08.
public
class
T {
09.
10.
public
int
add(
int
a,
int
b) {
11.
12.
return
a + b;
13.
}
14.
15.
public
double
div(
double
a,
double
b) {
16.
17.
return
a / b;
18.
}
19.
20.
public
String getName(String name) {
21.
22.
return
name;
23.
}
24.
25.
public
List<String> getList(String item) {
26.
27.
List<String> l =
new
ArrayList<String>();
28.
l.add(item);
29.
return
l;
30.
}
31.
32.
public
Map<String, String> getMap(String key, String value) {
33.
34.
Map<String, String> m =
new
HashMap<String, String>();
35.
m.put(key, value);
36.
return
m;
37.
}
38.
39.
}
TTest.java:
01.
package
cn.edu.ahau.mgc.junit4.test;
02.
03.
import
java.util.List;
04.
import
java.util.Map;
05.
06.
import
org.junit.Test;
07.
import
static
org.junit.Assert.*;
08.
import
cn.edu.ahau.mgc.junit4.T;
09.
import
static
org.hamcrest.Matchers.*;
10.
11.
public
class
TTest {
12.
13.
@Test
14.
public
void
testAdd() {
15.
16.
//通常匹配符
17.
int
s =
new
T().add(
1
,
1
);
18.
//allOf:全部條件必須都成立,測試才經過
19.
assertThat(s, allOf(greaterThan(
1
), lessThan(
3
)));
20.
//anyOf:只要有一個條件成立,測試就經過
21.
assertThat(s, anyOf(greaterThan(
1
), lessThan(
1
)));
22.
//anything:不管什麼條件,測試都經過
23.
assertThat(s, anything());
24.
//is:變量的值等於指定值時,測試經過
25.
assertThat(s, is(
2
));
26.
//not:和is相反,變量的值不等於指定值時,測試經過
27.
assertThat(s, not(
1
));
28.
29.
//數值匹配符
30.
double
d =
new
T().div(
10
,
3
);
31.
//closeTo:浮點型變量的值在3.0±0.5範圍內,測試經過
32.
assertThat(d, closeTo(
3.0
,
0.5
));
33.
//greaterThan:變量的值大於指定值時,測試經過
34.
assertThat(d, greaterThan(
3.0
));
35.
//lessThan:變量的值小於指定值時,測試經過
36.
assertThat(d, lessThan(
3.5
));
37.
//greaterThanOrEuqalTo:變量的值大於等於指定值時,測試經過
38.
assertThat(d, greaterThanOrEqualTo(
3.3
));
39.
//lessThanOrEqualTo:變量的值小於等於指定值時,測試經過
40.
assertThat(d, lessThanOrEqualTo(
3.4
));
41.
42.
//字符串匹配符
43.
String n =
new
T().getName(
"Magci"
);
44.
//containsString:字符串變量中包含指定字符串時,測試經過
45.
assertThat(n, containsString(
"ci"
));
46.
//startsWith:字符串變量以指定字符串開頭時,測試經過
47.
assertThat(n, startsWith(
"Ma"
));
48.
//endsWith:字符串變量以指定字符串結尾時,測試經過
49.
assertThat(n, endsWith(
"i"
));
50.
//euqalTo:字符串變量等於指定字符串時,測試經過
51.
assertThat(n, equalTo(
"Magci"
));
52.
//equalToIgnoringCase:字符串變量在忽略大小寫的狀況下等於指定字符串時,測試經過
53.
assertThat(n, equalToIgnoringCase(
"magci"
));
54.
//equalToIgnoringWhiteSpace:字符串變量在忽略頭尾任意空格的狀況下等於指定字符串時,測試經過
55.
assertThat(n, equalToIgnoringWhiteSpace(
" Magci "
));
56.
57.
//集合匹配符
58.
List<String> l =
new
T().getList(
"Magci"
);
59.
//hasItem:Iterable變量中含有指定元素時,測試經過
60.
assertThat(l, hasItem(
"Magci"
));
61.
62.
Map<String, String> m =
new
T().getMap(
"mgc"
,
"Magci"
);
63.
//hasEntry:Map變量中含有指定鍵值對時,測試經過
64.
assertThat(m, hasEntry(
"mgc"
,
"Magci"
));
65.
//hasKey:Map變量中含有指定鍵時,測試經過
66.
assertThat(m, hasKey(
"mgc"
));
67.
//hasValue:Map變量中含有指定值時,測試經過
68.
assertThat(m, hasValue(
"Magci"
));
69.
}
70.
71.
}