关于hamcrest断言的不足
hamcrest断言提供了很多好用的断言表达式,参见http://0411.iteye.com/blog/1048303
我最感兴趣的是对集合中Bean属性的断言,相信这也是最具实用的断言,可见hamcrest偏偏在这方面做得很弱啊,以下是我的一断测试代码:
@Test public void testNotSeniorQuery() throws Exception { List<ParsedQueryParam> parsedQueryParams = queryParamParser.parse(request); assertThat(parsedQueryParams, notNullValue()); assertThat(parsedQueryParams, hasSize(6)); assertThat(parsedQueryParams, hasItem(hasProperty("aaa",is("a")))); }
@Test public void testNotSeniorQuery() throws Exception { List parsedQueryParams = queryParamParser.parse(request);//这儿直接用List assertThat(parsedQueryParams, notNullValue()); assertThat(parsedQueryParams, hasSize(6)); assertThat((List<Object>)parsedQueryParams, //这儿将List cast 为List<Object> hasItem(hasProperty("aaa",is("a")))); }
@Test public void testNotSeniorQuery() throws Exception { List parsedQueryParams = queryParamParser.parse(request); assertThat(parsedQueryParams, notNullValue()); assertThat(parsedQueryParams, hasSize(6)); assertThat((List<Object>)parsedQueryParams, hasItem(hasProperty("queryParam.paramName",is("userId1"))));//失败,无法正确断言!! }
@Test public void testNotSeniorQuery() throws Exception { List parsedQueryParams = queryParamParser.parse(request); assertThat(parsedQueryParams, notNullValue()); assertThat(parsedQueryParams, hasSize(6)); assertThat((List<Object>)parsedQueryParams, hasItem(hasProperty("queryParam",hasProperty("paramName",is("userId1")))));//NND 这样不行! }