unitils库的使用经历

来源:互联网 发布:075566858233淘宝诈骗 编辑:程序博客网 时间:2024/04/27 18:42

unitils库的使用经历

maven坐标

    <dependency>        <groupId>org.unitils</groupId>        <artifactId>unitils-core</artifactId>        <version>3.4.2</version>        <scope>test</scope>    </dependency>

在单元测试里判断结果是否正确,使用这个库很方便,比如:

    Object expect = ...;    Object actual = ...;    ReflectionAssert.assertLenientEquals(expect, actual); 

这个Lenient宽松类型的比较函数对expect里,包括本身都是为默认值即忽略,比如expect里有int v; 而actual里有int v = 4;对v的断言是相等。
在实际使用中,遇到个有意思的报错,类似:

        Map<String, String> map = new HashMap<>();        map.put("1", "1");        Map<String, String> expect = new HashMap<>(map);        Map<String, String> actual = new HashMap<>(map);        actual.put("4", "4");        ReflectionAssert.assertLenientEquals(expect, actual);
junit.framework.AssertionFailedError: Expected: {"1"="1"}, actual: {"4"="4", "1"="1"}--- Found following differences ---"4": expected: "4", actual: ""--- Difference detail tree --- expected: {"1"="1"}   actual: {"4"="4", "1"="1"}"4" expected: "4""4"   actual: ""

最后两行,我原先以为我把期望和实际写反了,2333。
反复确认后,没毛病。看源码。

比较数据的操作其实是由几个Comparator对象控制的

org.unitils.reflectionassert.ReflectionComparatorFactory#getComparatorChain

这里的Comparator接口有两个方法:

 boolean canCompare(Object left, Object right); // 判断这个Comparator对象能否用于这组比较 Difference compare(Object left, Object right, boolean onlyFirstDifference, ReflectionComparator reflectionComparator); // 得出比较的差异结果

上面方法签名的参数,实际上left代表expected,right代表actual,已下相同。
实际比较时,会从中找出个canCompare返回true的Comparator实现。然后才会调compare返回差异。之所以会忽略掉expected里空值(或原生类型默认值)的比较,是因为IGNORE_DEFAULTS_COMPARATOR实现。

由于map的比较有专门的MAP_COMPARATOR,其中的值的比较结果是一下两种
1. 求键值对非相交的部分,即左边有,但右边没有,或者相反。
2. 键相同但值不同
Difference 对象实际上有多种值类型实现,通过不同的访问者去读取差异数据。
回到最初的问题:

– 测试 report是不是写反了?
– 嗯,写反了
org.unitils.reflectionassert.report.impl.DefaultDifferenceView#formatDifference(org.unitils.reflectionassert.difference.MapDifference, java.lang.String)
关注最后个for循环,以下是getRightMissingKeysformatValues的方法签名:

protected String formatValues(String fieldName, Object leftValue, Object rightValue);
    /**     * Gets the keys of the left maps that were missing in the right map.     * 右边有,左边却缺少的。也就是expected没有,actual却有的     */    public List<Object> getRightMissingKeys();

最后个for循环里,formatValues的后两个参数rightValue才应该是rightMap.get(rightKey)leftValue才应该是""
看起来这么回事,实际也确实如此,那么这个库历史这么悠久?一直没解决啊。Google下找到:

https://unitils.atlassian.net/browse/UNI-210

wrong report for MapDifference when left key is missing, right key is put to ‘expected’ value innstead of ‘actual’
2010年3.1的版本?那现在呢?2017年3.4.2的版本?一直没修复,233333。

原创粉丝点击