1 import static org.hamcrest.MatcherAssert.*;
2 import static org.hamcrest.CoreMatchers.*;
4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.junit.Before; 10 import org.junit.Test; 11 12 import com.lyh.share.model.User; 13 14 public class UserDaoTest { 15 16 private User test1; 17 private User test2; 18 19 @Before 20 public void init(){ 21 test1 = new User(); 22 test1.setUsername("tt1"); 23 test1.setPassword("123"); 24 test1.setShares(50); 25 test2 = new User(); 26 test2.setUsername("tt2"); 27 test2.setPassword("321"); 28 test2.setShares(20); 29 } 30 31 @Test 32 public void findUser(){ 33 34 /**数值匹配**/ 35 //测试变量是否大于指定值 36 assertThat(test1.getShares(), greaterThan(50)); 37 //测试变量是否小于指定值 38 assertThat(test1.getShares(), lessThan(100)); 39 //测试变量是否大于等于指定值 40 assertThat(test1.getShares(), greaterThanOrEqualTo(50)); 41 //测试变量是否小于等于指定值 42 assertThat(test1.getShares(), lessThanOrEqualTo(100)); 43 44 //测试所有条件必须成立 45 assertThat(test1.getShares(), allOf(greaterThan(50),lessThan(100))); 46 //测试只要有一个条件成立 47 assertThat(test1.getShares(), anyOf(greaterThanOrEqualTo(50), lessThanOrEqualTo(100))); 48 //测试无论什么条件成立(还没明白这个到底是什么意思) 49 assertThat(test1.getShares(), anything()); 50 //测试变量值等于指定值 51 assertThat(test1.getShares(), is(100)); 52 //测试变量不等于指定值 53 assertThat(test1.getShares(), not(50)); 54 55 /**字符串匹配**/ 56 String url = "http://www.taobao.com"; 57 //测试变量是否包含指定字符 58 assertThat(url, containsString("taobao")); 59 //测试变量是否已指定字符串开头 60 assertThat(url, startsWith("http://")); 61 //测试变量是否以指定字符串结尾 62 assertThat(url, endsWith(".com")); 63 //测试变量是否等于指定字符串 64 assertThat(url, equalTo("http://www.taobao.com")); 65 //测试变量再忽略大小写的情况下是否等于指定字符串 66 assertThat(url, equalToIgnoringCase("http://www.taobao.com")); 67 //测试变量再忽略头尾任意空格的情况下是否等于指定字符串 68 assertThat(url, equalToIgnoringWhiteSpace("http://www.taobao.com")); 69 70 71 /**集合匹配**/ 72 73 Listuser = new ArrayList (); 74 user.add(test1); 75 user.add(test2); 76 77 //测试集合中是否还有指定元素 78 assertThat(user, hasItem(test1)); 79 assertThat(user, hasItem(test2)); 80 81 /**Map匹配**/ 82 Map userMap = new HashMap (); 83 userMap.put(test1.getUsername(), test1); 84 userMap.put(test2.getUsername(), test2); 85 86 //测试map中是否还有指定键值对 87 assertThat(userMap, hasEntry(test1.getUsername(), test1)); 88 //测试map中是否还有指定键 89 assertThat(userMap, hasKey(test2.getUsername())); 90 //测试map中是否还有指定值 91 assertThat(userMap, hasValue(test2)); 92 } 93 94 }
㈠ 一般匹配符
1、allOf
匹配符表明如果接下来的所有条件必须都成立测试才通过,相当于“与”(&&)
assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) );
2、anyOf
匹配符表明如果接下来的所有条件只要有一个成立则测试通过,相当于“或”(||)
assertThat( testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
3、anything
匹配符表明无论什么条件,永远为true
assertThat( testedNumber, anything() );
4、is
匹配符表明如果前面待测的object等于后面给出的object,则测试通过
assertThat( testedString, is( "developerWorks" ) );
5、not
匹配符和is匹配符正好相反,表明如果前面待测的object不等于后面给出的object,则测试通过
assertThat( testedString, not( "developerWorks" ) );
㈡ 字符串相关匹配符
1、containsString
匹配符表明如果测试的字符串testedString 包含 子字符串"developerWorks"则测试通过
assertThat( testedString, containsString( "developerWorks" ) );
2、endsWith
匹配符表明如果测试的字符串testedString以子字符串"developerWorks"结尾则测试通过
assertThat( testedString, endsWith( "developerWorks" ) );
3、startsWith
匹配符表明如果测试的字符串testedString以子字符串"developerWorks"开始则测试通过
assertThat( testedString, startsWith( "developerWorks" ) );
4、equalTo
匹配符表明如果测试的testedValue等于expectedValue则测试通过,equalTo可以测试数值之间,字符串
之间和对象之间是否相等,相当于Object的equals方法
assertThat( testedValue, equalTo( expectedValue ) );
5、equalToIgnoringCase
匹配符表明如果测试的字符串testedString在忽略大小写的情况下等于"developerWorks"则测试通过
assertThat( testedString, equalToIgnoringCase( "developerWorks" ) );
6、equalToIgnoringWhiteSpace
匹配符表明如果测试的字符串testedString在忽略头尾的任意个空格的情况下等于"developerWorks"则
测试通过,注意:字符串中的空格不能被忽略
assertThat( testedString, equalToIgnoringWhiteSpace( "developerWorks" ) );
㈢ 数值相关匹配符
1、closeTo
匹配符表明如果所测试的浮点型数testedDouble在20.0±0.5范围之内则测试通过
assertThat( testedDouble, closeTo( 20.0, 0.5 ) );
2、greaterThan
匹配符表明如果所测试的数值testedNumber大于16.0则测试通过
assertThat( testedNumber, greaterThan(16.0) );
3、lessThan
匹配符表明如果所测试的数值testedNumber小于16.0则测试通过
assertThat( testedNumber, lessThan (16.0) );
4、greaterThanOrEqualTo
匹配符表明如果所测试的数值testedNumber大于等于16.0则测试通过
assertThat( testedNumber, greaterThanOrEqualTo (16.0) );
5、lessThanOrEqualTo
匹配符表明如果所测试的数值testedNumber小于等于16.0则测试通过
assertThat( testedNumber, lessThanOrEqualTo (16.0) );
㈣ collection相关匹配符
1、hasEntry
匹配符表明如果测试的Map对象mapObject含有一个键值为"key"对应元素值为"value"的Entry项则测试通过
assertThat( mapObject, hasEntry( "key", "value" ) );
2、hasItem
匹配符表明如果测试的迭代对象iterableObject含有元素“element”项则测试通过
assertThat( iterableObject, hasItem ( "element" ) );
3、hasKey
匹配符表明如果测试的Map对象mapObject含有键值“key”则测试通过
assertThat( mapObject, hasKey ( "key" ) );
4、hasValue
匹配符表明如果测试的Map对象mapObject含有元素值“value”则测试通过
assertThat( mapObject, hasValue ( "key" ) );