论坛首页 Java企业应用论坛

关于hamcrest断言的不足

浏览 1871 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-12-04   最后修改:2013-12-05
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"))));
    }


  以上代码直接通不过编译,具体解决方法参见:
https://weblogs.java.net/blog/johnsmart/archive/2008/04/on_the_subtle_u.html
  也即,以上测试代码必须改成:
    @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"))));
    }


   虽然这样可以通过编译了,但是我还有更多类似于以下的断言集合中Bean级联属性的需求:
即ParsedQueryParam类包含了一个QueryParam,QueryParam中拥有paramName及paramValue属性,我希望hamcrest能够支持以下的代码:
    @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"))));//失败,无法正确断言!!
    }


   可是这样无法成功运行,报如下错误:
引用

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:31)
at org.hamcrest.core.IsCollectionContaining.matchesSafely(IsCollectionContaining.java:14)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:12)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at com.hsit.euler.eqengine.query.parser.impl.QueryParamParserImplTest.testNotSeniorQuery(QueryParamParserImplTest.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:200)
at org.unitils.UnitilsTestNG.run(UnitilsTestNG.java:103)


   此路不通,我就试着换成如下方式:
    @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"))));//这样可以成功断言!

    }

   但是以上断言只是断言属性的存在性,如果我断言属性值,则在写成:
    @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("userId")))));

    }

    虽然是可以了,但是如果级联属性的层级比较多,真是好不麻烦啊!

   一直想找一个可以很好对集合中Bean进行存在性断言或Bean属性值断言的框架,发现hamcrest很让人失望啊,要是能支持类似于就太棒了:
  assertThat((List<Object>)parsedQueryParams,
                hasItem(hasProperty("attr1.attr11.attr111",is("userId"))));

不知列位有没有好的建议,非常感谢!
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics