JUnit Assert with BigDecimal

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

    The official junit solution to assert that two BigDecimal are matematically equal is to use hamcrest.

    With java-hamcrest 2.0.0.0 we can use this syntax:

        // import static org.hamcrest.MatcherAssert.assertThat;
        // import org.hamcrest.Matchers;
    
        BigDecimal a = new BigDecimal("100")
        BigDecimal b = new BigDecimal("100.00")
        assertThat(a,  Matchers.comparesEqualTo(b));
    

    Hamcrest 1.3 Quick Reference

提交回复
热议问题