JUnit Assert with BigDecimal

后端 未结 9 2002
遇见更好的自我
遇见更好的自我 2021-02-02 05:47

I want to use assert between 2 two decimal, I use this:

BigDecimal bd1 = new BigDecimal (1000);
BigDecimal bd2 = new BigDecimal (1000);
org.junit.Assert.assertSa         


        
9条回答
  •  抹茶落季
    2021-02-02 06:15

    The method assertSame tests that both are the same object. However, you have two objects which have the same value. To test this, you can use assertEquals.

    However, you should be aware of some unexpected behavior when using assertEquals (which depends on the equals method) on BigDecimals. For example, new BigDecimal("100").divide(new BigDecimal("10.0")).equals(new BigDecimal("10")) evaluates to false because equals also looks at the scale of the BigDecimal instances.

    In many circumstances it is better to compare BigDecimals by using the compareTo method:

    assertTrue(bd1.compareTo(bd2) == 0);
    

提交回复
热议问题