使用DbUnit将BigDecimal数据放入HSQLDB测试数据库

JMM*_*JMM 5 testing dbunit hibernate hsqldb

我在我的后端使用Hibernate JPA.我正在使用JUnit和DBUnit编写单元测试,以将一组数据插入到内存中的HSQL数据库中.

我的数据集包含:

<order_line order_line_id="1" quantity="2" discount_price="0.3"/>
Run Code Online (Sandbox Code Playgroud)

它映射到OrderLine Java对象,其中discount_price列定义为:

@Column(name = "discount_price", precision = 12, scale = 2)
private BigDecimal discountPrice;
Run Code Online (Sandbox Code Playgroud)

但是,当我运行我的测试用例并声明返回的折扣价格等于0.3时,断言失败并表示存储的值为0.如果我将数据集中的discount_price更改为0.9,则它会向上舍入为1.

我已经检查过以确保HSQLDB没有进行舍入,这肯定不是因为我可以使用类似5.3的值的Java代码插入订单行对象并且它工作正常.

对我来说,似乎DBUtils由于某种原因舍入了我定义的数字.有没有办法可以迫使这种情况发生?任何人都可以解释为什么它可能这样做?

谢谢!

Pas*_*ent -1

我使用 DbUnit 2.2.2、HSQLDB 1.8.0.10 和 Hibernate EM 3.4.0.GA 进行了快速测试,一切正常:

  • DbUnit 将十进制值正确插入到discount_pricetype 的列中numeric(12,2)
  • Hibernate 正确地将discount_price值读取到BigDecimal discountPrice
  • 以下assertEquals(0.3d, orderLine.getDiscountPrice().doubleValue())通行证

所以我的问题是:

  • 您到底使用哪个版本的 DbUnit?
  • 你到底如何做你的断言?你们也有 DbUnit API 吗?