assertThat
Hamcrest一直是被墙的
打开junit的包看看,发现有 org.hamcrest.CoreMatchers
用了用,感觉没有什么两样
再google下
原来这个CoreMatchers相当于org.hamcrest.Matchers的缩水版
还是要去官网down 那个core 和 library的包
从别人那里copy个用法list
T.java:
package cn.edu.ahau.mgc.junit4;
02.
03.import java.util.List;
04.import java.util.ArrayList;
05.import java.util.HashMap;
06.import java.util.Map;
07.
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.*; //import static org.hamcrest.CoreMatchers.*;
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.}