views:

133

answers:

1

Hi everyone,

I'm using Hibernate JPA in my backend. I am writing a unit test using JUnit and DBUnit to insert a set of data into an in-memory HSQL database.

My dataset contains:

<order_line order_line_id="1" quantity="2" discount_price="0.3"/>

Which maps to an OrderLine Java object where the discount_price column is defined as:

@Column(name = "discount_price", precision = 12, scale = 2)
private BigDecimal discountPrice;

However, when I run my test case and assert that the discount price returned equals 0.3, the assertion fails and says that the stored value is 0. If I change the discount_price in the dataset to be 0.9, it rounds up to 1.

I've checked to make sure HSQLDB isn't doing the rounding and it definitely isn't because I can insert an order line object using Java code with a value like 5.3 and it works fine.

To me, it seems like DBUtils is for some reason rounding the number I've defined. Is there a way I can force this to not happen? Can anyone explain why it might be doing this?

Thanks!

A: 

I did a quick test on my side with DbUnit 2.2.2, HSQLDB 1.8.0.10 and Hibernate EM 3.4.0.GA and things are working as expected:

  • DbUnit correctly inserts decimal values into a discount_price column of type numeric(12,2)
  • Hibernate correctly reads the discount_price values into a BigDecimal discountPrice
  • the following assertEquals(0.3d, orderLine.getDiscountPrice().doubleValue()) passes

So my questions are:

  • What version of DbUnit do you use exactly?
  • How do you do your assert exactly? Do you DbUnit API for that too?
Pascal Thivent