DbUnit断言浮点数 [英] DbUnit Assertion floating-point numbers

查看:127
本文介绍了DbUnit断言浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DbUnit测试我的DAO层.我正在从XML数据集中预填充数据库,执行一些操作,然后根据已知结果进行断言.

I'm testing my DAO layer using DbUnit. I'm prefilling database from XML dataset, doing some actions and then asserting against known result.

Assertion.assertEquals(expectedDataSet, actualDataSet);

数据集包含带有浮点数的列.然后比较这些列,我得到:

Dataset contains column with floating point number. Then these columns are compared, I get:

junit.framework.ComparisonFailure: value (table=OrderLine_T, row=2, col=price) expected:<2.99[]> but was:<2.99[0000009536743]>.

值是相等的,但是因为不能以二进制形式精确表示浮点数,所以断言失败. 在JUnit中,我们有assertEquals(double expected, double actual, double delta).我们如何在DbUnit中设置一些增量以进行浮点数比较?

The values are equal, but because floating-point numbers cannot be exactly represented in binary form, the assertion fails. In JUnit we have assertEquals(double expected, double actual, double delta). How do we set some delta in DbUnit for floating-point number comparison ?

初始数据集:

<dataset>
    <Customer_T id="1" name="Anthony"/>
    <Customer_T id="2" name="John"/>

    <Order_T id="1" date="2012-06-07 14:30" customer_id="1" />
    <Order_T id="2" date="2012-06-07 15:31" customer_id="2" />

    <OrderLine_T id="1" order_id="1" product_id="1" price="2.99" quantity="5" />
    <OrderLine_T id="2" order_id="2" product_id="2" price="3.49" quantity="10" />
</dataset>

预期结果:

<dataset>
    <Customer_T id="1" name="Anthony"/>
    <Customer_T id="2" name="John"/>

    <Order_T id="1" date="2012-06-07 14:30" customer_id="1" />
    <Order_T id="2" date="2012-06-07 15:31" customer_id="2" />

    <OrderLine_T id="1" order_id="1" product_id="1" price="2.99" quantity="5" />
    <OrderLine_T id="2" order_id="2" product_id="2" price="3.49" quantity="10" />

<!--     Below added -->
    <Order_T id="3" date="1987-06-07 9:15:10" customer_id="2" />
    <OrderLine_T id="3" order_id="3" product_id="1" price="2.99" quantity="2" />
    <OrderLine_T id="4" order_id="3" product_id="5" price="3.55" quantity="8" />
</dataset>

代码:

/* Should save order correctly (including order lines) */
    @Test
    public void save() throws Exception {
        /* Create new order */
        Set<OrderLine> lines = new HashSet<OrderLine>();
        lines.add(new OrderLine(1, (float)2.99, 2));
        lines.add(new OrderLine(5, (float)3.55, 8));

        Calendar cal = Calendar.getInstance();
        cal.set(1987, 6, 7, 9, 15, 10);
        Date date = cal.getTime();

        Customer customer = customerDAO.findById(2); // John

        Order order = new Order(date, lines, customer);
        orderDAO.save(order);
        entityManager.flush();

        /* Assert order is saved */
        IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(Thread.currentThread()
                                                                           .getContextClassLoader()
                                                                           .getResourceAsStream("data-set-afterAddOrder.xml"));
        IDataSet actualDataSet = getDatabaseConnection().createDataSet();

        Assertion.assertEquals(expectedDataSet, actualDataSet);
    }

可能需要提及我正在使用的内存中HSQLDB.刚刚尝试过MySQL,它就成功通过了.

Probably need to mention what I'm using in-memory HSQLDB. Just tried MySQL and it passes successfully.

尝试设置ToleratedDelta失败:

IDatabaseConnection connection = new DatabaseConnection(((SessionImpl) (entityManager.getDelegate())).connection());
HsqldbDataTypeFactory dataTypeFactory = new HsqldbDataTypeFactory();
dataTypeFactory.addToleratedDelta(new ToleratedDelta("OrderLine_T", "price", 0.01));
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory);

Edit2:

我正在使用hibernate.hbm2ddl.auto = create将架构导出到数据库.从 fredt 了解想法后,我将实体上的字段 price 的类型更改为BigDecimal,并添加了其他列精度和小数位参数:

I was using hibernate.hbm2ddl.auto = create, to export schema to database. After got idea from fredt, I changed the type of field price on my entity to BigDecimal and added additional column precision and scale params:

@Column(precision=10, scale=4)
private BigDecimal price;

此内容翻译为PRICE NUMERIC(10,4).通过 fredt 解决了问题.

This gets translated to PRICE NUMERIC(10,4). Problem solved, thanks to fredt.

推荐答案

如果将列定义为DECIMAL或NUMERIC,且具有所需的精度和小数位数,则该值将在小数点后具有数字的加倍数,并且问题可以避免.

If the column is defined as DECIMAL or NUMERIC with the required precision and scale, the value will have the exace number of digits after the decimal point and the issue can be avoided.

这篇关于DbUnit断言浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆