您的位置:首页 > 其它

unitils库的使用经历

2017-08-13 22:35 106 查看

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对象控制的



这里的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是不是写反了?

– 嗯,写反了



关注最后个for循环,以下是
getRightMissingKeys
formatValues
的方法签名:

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下找到:



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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unitils bug 源码