JUnit assertions : make the assertion between floats

馋奶兔 提交于 2019-12-02 21:45:35

You have to provide a delta to the assertion for Floats:

Assert.assertEquals(expected, actual, delta)

While delta is the maximum difference (delta) between expected and actual for which both numbers are still considered equal.

Assert.assertEquals(0.0012f, 0.0014f, 0.0002); // true
Assert.assertEquals(0.0012f, 0.0014f, 0.0001); //false

A delta-value of 0.0f also works, so for old fashioned "==" compares (use with care!), you can write

Assert.assertEquals(expected, actual, 0.0f);

instead of

Assert.assertEquals(expected, actual); // Deprecated
Assert.assertTrue(expected == actual); // Not JUnit

I like the way JUnit ensures that you really thought about the "delta" which should only be 0.0f in really trivial cases.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!