Is it ok to use assertTrue on float and double values in JUnit?

前端 未结 1 881
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 06:26

I tried using the assertEquals method on a float and eclipse says that the method was depreciated. Therefore, is the following alternative acceptable?

Assert.ass         


        
相关标签:
1条回答
  • 2021-01-23 07:22

    In general you can compare primitive types with ==, but with floating point values, two values can look the same (when formatted as a string) but be two different values. You should be using assertEquals(expected, actual, delta). See the answer to JUnit: assertEquals for double values.

    Because double (and float) calculations are not necessarily exact, you can compare using a delta (effectively a margin of error).

    0 讨论(0)
提交回复
热议问题