JUnit Assert with BigDecimal

后端 未结 9 1974
遇见更好的自我
遇见更好的自我 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:19

    bd1 and bd2 are two different objects, and since assertSame checks the object reference using the == operator, you're getting that message, see the docs:

    Asserts that two objects refer to the same object. If they are not the same, an AssertionError without a message is thrown.

    You should use assertEquals instead, it checks that the two objects are equal - which is what you want.


    Note that comparing two BigDecimal objects using the == operator will work as long as their values are cached (for 0 through 10) values.

提交回复
热议问题