[英]JUnit assertions : make the assertion between floats
I need to compare two values : one a string and the other is float so I convert the string to float then try to call assertEquals(val1,val2)
but this is not authorized , I guess that the assertEquals
doesn't accept float as arguments. 我需要比较两个值:一个是字符串,另一个是浮点数,所以我将字符串转换为float然后尝试调用
assertEquals(val1,val2)
但是这没有被授权,我想assertEquals
不接受float作为参数。
What is the solution for me in this case ? 在这种情况下,我的解决方案是什么?
You have to provide a delta to the assertion for Floats: 您必须为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. 而delta是预期值与实际值之间的最大差值(delta),两个数值仍被视为相等。
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 delta值为0.0f也有效,所以对于老式的“==”比较(小心使用!),你可以写
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. 我喜欢JUnit确保你真正考虑过“delta”的方式,它在真正无关紧要的情况下应该只有0.0f。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.