Java 为什么在 JUnit 中不推荐使用 assertEquals(double,double)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33274030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is assertEquals(double,double) deprecated in JUnit?
提问by jsh6303
I was wondering why assertEquals(double, double)
is deprecated.
我想知道为什么assertEquals(double, double)
不推荐使用。
I used import static org.junit.Assert.assertEquals;
and I used JUnit 4.11.
我用过import static org.junit.Assert.assertEquals;
,我用过 JUnit 4.11。
Below is my code:
下面是我的代码:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class AccountTest {
@Test
public void test() {
Account checking = new Account(Account.CHECKING);
checking.deposit(1000.0);
checking.withdraw(100.0);
assertEquals(900.0, checking.getBalance());
}
}
checking.getBalance()
returns a double value.
checking.getBalance()
返回一个双精度值。
What could be wrong?
可能有什么问题?
采纳答案by Codebender
It's deprecated because of the double's precision problems.
由于双精度问题,它已被弃用。
If you note, there's another method assertEquals(double expected, double actual, double delta)
which allows a delta
precision loss.
如果您注意到,还有另一种方法会assertEquals(double expected, double actual, double delta)
导致delta
精度损失。
Asserts that two doubles are equal to within a positive delta. If they are not, an AssertionError is thrown. If the expected value is infinity then the delta value is ignored.NaNs are considered equal:
assertEquals(Double.NaN, Double.NaN, *)
passes...
delta - the maximum delta between expected and actual for which both numbers are still considered equal.
断言两个双精度值在正增量内相等。如果不是,则抛出 AssertionError。如果预期值为无穷大,则忽略 delta 值。 NaN 被认为是相等的:
assertEquals(Double.NaN, Double.NaN, *)
通过...
delta - 两个数字仍被视为相等的预期和实际之间的最大增量。
回答by Eric
assertEquals(double, double)
is deprecated because the 2 doubles may be the same but if they are calculated values, the processor may make them slightly different values.
assertEquals(double, double)
已弃用,因为 2 个双精度值可能相同,但如果它们是计算值,则处理器可能会使它们的值略有不同。
If you try this it will fail: assertEquals(.1 + .7, .8)
. This was tested using an Intel? processor.
如果您尝试此操作,它将失败:assertEquals(.1 + .7, .8)
。这是使用英特尔测试的吗?处理器。
Calling the deprecated methodwill trigger fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers");
to be called.
调用已弃用的方法将触发fail("Use assertEquals(expected, actual, delta) to compare floating-point numbers");
被调用。
回答by Victor Augusto
People explain but don't give samples... So here goes what worked for me:
人们解释但不提供样品......所以这对我有用:
@Test
public void WhenMakingDepositAccountBalanceIncreases() {
Account account = new Account();
account.makeDeposit(10.0);
assertEquals("Account balance was not correct.", 10.0, account.getBalance(), 0);
}
The 0
in the end;
该0
到底;
回答by steven35
Old question but this hasn't been said yet and might help someone.
老问题,但这还没有说,可能会帮助某人。
You can use com.google.common.math.DoubleMath.fuzzyEquals(double a, double b, double tolerance)
which allows you to specify how close the two doubles should be to each other.
您可以使用com.google.common.math.DoubleMath.fuzzyEquals(double a, double b, double tolerance)
which 允许您指定两个双打应该彼此接近的程度。
I found it very handy for unit tests where I don't want to hardcode test result values with a lot of decimal places.
我发现它对于单元测试非常方便,我不想用很多小数位对测试结果值进行硬编码。