为什么在JUnit中不推荐使用assertEquals(double,double)? [英] Why is assertEquals(double,double) deprecated in JUnit?

查看:636
本文介绍了为什么在JUnit中不推荐使用assertEquals(double,double)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么不推荐使用assertEquals(double, double).

I was wondering why assertEquals(double, double) is deprecated.

我使用了import static org.junit.Assert.assertEquals;,而我使用了JUnit 4.11.

I used import static org.junit.Assert.assertEquals; and I used JUnit 4.11.

下面是我的代码:

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()返回一个双精度值.

有什么问题吗?

推荐答案

由于double的精度问题而已弃用.

It's deprecated because of the double's precision problems.

请注意,还有另一种方法assertEquals(double expected, double actual, double delta)允许delta精度损失.

If you note, there's another method assertEquals(double expected, double actual, double delta) which allows a delta precision loss.

JavaDoc :

断言两个双精度数等于正增量之内.如果不是,则会引发AssertionError.如果期望值是无穷大,则将忽略增量值.NaN被视为相等:assertEquals(Double.NaN, Double.NaN, *)通过

...

delta-期望值与实际值之间的最大差额,对于这两个数字,仍然认为是相等的.

delta - the maximum delta between expected and actual for which both numbers are still considered equal.

这篇关于为什么在JUnit中不推荐使用assertEquals(double,double)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆