Entering BigDecimal data into the HSQLDB test database using DbUnit

I use Hibernate JPA in my backend. I am writing unit test using JUnit and DBUnit to insert a dataset into an in-memory HSQL database.

My dataset contains:

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

What the OrderLine Java object displays, 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 claim that the discount price returned is 0.3, the statement fails and says that the stored value is 0. If I change the discount_price in the dataset to 0.9, it is rounded to 1.

I made sure that HSQLDB does not perform rounding, and this is definitely not because I can insert an order line object using Java code with a value of 5.3, and it works fine.

It seems to me that DBUtils for some reason rounds the number I determined. Is there a way to make this not happen? Can anyone explain why this can be done?

Thanks!

+4
source share
3 answers

I came across this question, since I was struggling with a similar problem, unfortunately, in my current setup, I can not update sleep mode. So I found another solution that fixes the problem without the need to update sleep mode. Thought it wouldn't hurt. You just extend HSQLDialect

 public class HSQLNumericWithPrecisionDialect extends HSQLDialect { public HSQLNumericWithPrecisionDialect() { super(); registerColumnType( Types.NUMERIC, "numeric($p, $s)" ); } } 
+2
source

I had a similar problem ... fixed with upgrade to Hibernate 3.6

+1
source

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 everything works as expected:

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

So my questions are:

  • Which version of DbUnit are you using for sure?
  • How do you say exactly? Do you use the DbUnit API too?
0
source

Source: https://habr.com/ru/post/1310340/


All Articles