我在断言方面有一个小问题,或者可能只是在 BigDecimal 方面有一个小问题。
我的 JUnit 测试在使用 assertEquals(Object expected, Object actual)
时抛出错误:
java.lang.AssertionError: expected:<10> but was:<10.000000000>
预期是通过以下方式创建的:
BigDecimal expected = MathHelper.getDecimal(10);
getDecimal
MathHelper
中的方法看起来像这样:
public static final BigDecimal getDecimal(long value) {
return BigDecimal.valueOf(value);
}
actual
是 private BigDecimal count
它的 getter 方法是一个经典的 getter 方法:
public BigDecimal getCount() {
return count;
}
我完全不知道这里发生了什么......
最佳答案
看看documentation of the equals
method for BigDecimal
:
Compares this BigDecimal with the specified Object for equality. Unlike
compareTo
, this method considers twoBigDecimal
objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).
所以不,10
和 10.000000000
不相等,断言错误是正确的。
关于java - Mockito 和 Assertions - 两个相同的 BigDecimals...不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33456540/