AssertJ - 方法isEqualToComparingFieldByFieldRecursively不使用RuntimeException对象

时间:2018-04-10 09:28:06

标签: java unit-testing testing junit assertj

摘要

方法isEqualToComparingFieldByFieldRecursively不使用RuntimeException对象。即使我们在预期的RuntimeException中更改RuntimeException消息,检查也始终返回true。

不工作的例子(总是如此)

assertThat(caughtException).as("The Runtime exception")
            .usingComparatorForType(ComparatorCollection.getDateComparator(MAX_ALLOWABLE_SEC_FOR_EQ),
                    LocalDateTime.class)
            .usingComparatorForType(ComparatorCollection.getXmlDateComparator(MAX_ALLOWABLE_SEC_FOR_EQ),
                    XMLGregorianCalendar.class)
            .isEqualToComparingFieldByFieldRecursively(expectedResults));
            // caughtException.message and expectedResults.message are different but return true

工作示例

assertThat(caughtException).as("The Runtime exception")
            .usingComparatorForType(ComparatorCollection.getDateComparator(MAX_ALLOWABLE_SEC_FOR_EQ),
                    LocalDateTime.class)
            .usingComparatorForType(ComparatorCollection.getXmlDateComparator(MAX_ALLOWABLE_SEC_FOR_EQ),
                    XMLGregorianCalendar.class)
            .isEqualToComparingFieldByFieldRecursively(expectedResults)
            // The recursively check don't work with the RuntimeException message
            .isEqualToComparingOnlyGivenFields(expectedResults, "message");

我们已经做了一些测试,第二个例子工作(当我们强制检查attribut消息时)。

1 个答案:

答案 0 :(得分:1)

这是检索要比较的字段的代码的旧限制,忽略java.lang中的任何类型(以避免按字段比较Object字段)。

这太激烈了,需要放松,已经创建了这个bug来跟踪问题https://github.com/joel-costigliola/assertj-core/issues/1224