首页 > 代码库 > Junit4中的新断言assertThat的是用方法

Junit4中的新断言assertThat的是用方法

如果需要是用assertThat需要在项目中引入junit4的jar包,以及hamcrest-core.jar和hamcrest-library.jar
下面是常用断言的代码

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
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
importstatic org.hamcrest.Matchers.*;
 importstatic org.junit.Assert.assertThat;
  
 importjava.util.ArrayList;
 importjava.util.HashMap;
 importjava.util.List;
 importjava.util.Map;
  
 importorg.junit.Before;
 importorg.junit.Test;
  
 importcom.lyh.share.model.User;
  
 publicclass UserDaoTest {
          
         privateUser test1;
         privateUser test2;
          
         @Before
         publicvoid init(){
                 test1 = newUser();
                 test1.setUsername("tt1");
                 test1.setPassword("123");
                 test1.setShares(50);
                 test2 = newUser();
                 test2.setUsername("tt2");
                 test2.setPassword("321");
                 test2.setShares(20);
         }
          
         @Test
         publicvoid findUser(){
                  
                 /**数值匹配**/
                 //测试变量是否大于指定值
                 assertThat(test1.getShares(), greaterThan(50));
                 //测试变量是否小于指定值
                 assertThat(test1.getShares(), lessThan(100));
                 //测试变量是否大于等于指定值
                 assertThat(test1.getShares(), greaterThanOrEqualTo(50));
                 //测试变量是否小于等于指定值
                 assertThat(test1.getShares(), lessThanOrEqualTo(100));
                  
                 //测试所有条件必须成立
                 assertThat(test1.getShares(), allOf(greaterThan(50),lessThan(100)));
                 //测试只要有一个条件成立
                 assertThat(test1.getShares(), anyOf(greaterThanOrEqualTo(50), lessThanOrEqualTo(100)));
                 //测试无论什么条件成立(还没明白这个到底是什么意思)
                 assertThat(test1.getShares(), anything());
                 //测试变量值等于指定值
                 assertThat(test1.getShares(), is(100));
                 //测试变量不等于指定值
                 assertThat(test1.getShares(), not(50));
                  
                 /**字符串匹配**/
                 String url = "http://www.taobao.com";
                 //测试变量是否包含指定字符
                 assertThat(url, containsString("taobao"));
                 //测试变量是否已指定字符串开头
                 assertThat(url, startsWith("http://"));
                 //测试变量是否以指定字符串结尾
                 assertThat(url, endsWith(".com"));
                 //测试变量是否等于指定字符串
                 assertThat(url, equalTo("http://www.taobao.com"));
                 //测试变量再忽略大小写的情况下是否等于指定字符串
                 assertThat(url, equalToIgnoringCase("http://www.taobao.com"));
                 //测试变量再忽略头尾任意空格的情况下是否等于指定字符串
                 assertThat(url, equalToIgnoringWhiteSpace("http://www.taobao.com"));
                  
                  
                 /**集合匹配**/
                  
                 List<User> user = newArrayList<User>();
                 user.add(test1);
                 user.add(test2);
                  
                 //测试集合中是否还有指定元素
                 assertThat(user, hasItem(test1));
                 assertThat(user, hasItem(test2));
  
                 /**Map匹配**/
                 Map<String,User> userMap = newHashMap<String,User>();
                 userMap.put(test1.getUsername(), test1);
                 userMap.put(test2.getUsername(), test2);
                  
                 //测试map中是否还有指定键值对
                 assertThat(userMap, hasEntry(test1.getUsername(), test1));
                 //测试map中是否还有指定键
                 assertThat(userMap, hasKey(test2.getUsername()));
                 //测试map中是否还有指定值
                 assertThat(userMap, hasValue(test2));
         }
          
 }

Junit4中的新断言assertThat的是用方法