1 import static org.hamcrest.MatcherAssert.*; 2 import static org.hamcrest.CoreMatchers.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.Before;
import org.junit.Test; import com.lyh.share.model.User; public class UserDaoTest { private User test1;
private User test2; @Before
public void init(){
test1 = new User();
test1.setUsername("tt1");
test1.setPassword("123");
test1.setShares(50);
test2 = new User();
test2.setUsername("tt2");
test2.setPassword("321");
test2.setShares(20);
} @Test
public void 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 = new ArrayList<User>();
user.add(test1);
user.add(test2); //测试集合中是否还有指定元素
assertThat(user, hasItem(test1));
assertThat(user, hasItem(test2)); /**Map匹配**/
Map<String,User> userMap = new HashMap<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));
} }
㈠ 一般匹配符
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" ) );