Java 带有 BigDecimal 的 JUnit 断言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35573187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:51:01  来源:igfitidea点击:

JUnit Assert with BigDecimal

javajunitassertbigdecimal

提问by kAnGeL

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.assertSame (bd1,bd2);

but the JUnit log shows:

但 JUnit 日志显示:

expected <1000> was not: <1000>

采纳答案by Tunaki

assertSametests that the two objects are the same objects, i.e. that they are ==:

assertSame测试两个对象是否是相同的对象,即它们是==

Asserts that two objects refer to the same object. If they are not the same, an AssertionErrorwithout a message is thrown.

断言两个对象引用同一个对象。如果它们不相同,AssertionError则抛出一个不带消息的消息。

In your case, since bd1and bd2are both new BigDecimal, the objects aren't the same, hence the exception.

在您的情况下,由于bd1bd2都是 new BigDecimal,因此对象不相同,因此是例外。

What you want is to use assertEquals, that tests if two objects are equal, i.e. .equals:

你想要的是使用assertEquals,测试两个对象是否相等,即.equals

Asserts that two objects are equal. If they are not, an AssertionErrorwithout a message is thrown. If expected and actual are null, they are considered equal.

断言两个对象相等。如果不是,AssertionError则抛出一个不带消息的消息。如果预期和实际是null,则认为它们相等。

BigDecimal bd1 = new BigDecimal (1000);
BigDecimal bd2 = new BigDecimal (1000);
org.junit.Assert.assertEquals(bd1,bd2);

回答by Maroun

bd1and bd2are two differentobjects, and since assertSamechecks the object referenceusing the ==operator, you're getting that message, see the docs:

bd1andbd2是两个不同的对象,并且由于使用运算符assertSame检查对象引用==,因此您会收到该消息,请参阅文档:

Asserts that two objects refer to the same object. If they are not the same, an AssertionErrorwithout a message is thrown.

断言两个对象引用同一个对象。如果它们不相同,AssertionError则抛出一个不带消息的消息。

You should use assertEqualsinstead, it checks that the two objects are equal - which is what you want.

您应该改用assertEquals它,它会检查两个对象是否相等 - 这就是您想要的。



Note that comparing two BigDecimalobjects using the ==operator will work as long as their values are cached (for 0 through 10) values.

请注意,只要它们的值被缓存(0 到 10)值,就可以BigDecimal使用==运算符比较两个对象。

回答by Vikrant Kashyap

Use AssertEqualsinstead of AssertSame... reason because assertequalschecks the value but assertsamechecks the refrence..

使用AssertEquals而不是AssertSame...原因因为assertequals检查值但assertsame检查参考..

回答by Hoopje

The method assertSametests that both are the same object. However, you have two objects which have the same value. To test this, you can use assertEquals.

该方法assertSame测试两者是同一个对象。但是,您有两个具有相同值的对象。要对此进行测试,您可以使用assertEquals.

However, you should be aware of some unexpected behavior when using assertEquals(which depends on the equalsmethod) on BigDecimals. For example, new BigDecimal("100").divide(new BigDecimal("10.0")).equals(new BigDecimal("10"))evaluates to falsebecause equalsalso looks at the scale of the BigDecimalinstances.

但是,在sassertEquals上使用(取决于equals方法)时,您应该注意一些意外行为BigDecimal。例如,new BigDecimal("100").divide(new BigDecimal("10.0")).equals(new BigDecimal("10"))评估为false因为equals还查看BigDecimal实例的规模。

In many circumstances it is better to compare BigDecimals by using the compareTomethod:

在许多情况下,最好BigDecimal使用以下compareTo方法比较s :

assertTrue(bd1.compareTo(bd2) == 0);

回答by Endery

assertSamechecks if both objects are the same instance. assertEqualschecks if the numbers are equal in value and scale, that means i.e. 1000 is not equal to 1000.00. If you want to compare only the numeric value, you should use compareTo()method from BigDecimal.

assertSame检查两个对象是否是同一个实例。assertEquals检查数字的值和比例是否相等,即 1000 不等于 1000.00。如果只想比较数值,则应使用compareTo()方法 from BigDecimal

For example:

例如:

BigDecimal bd1 = new BigDecimal (1000.00);
BigDecimal bd2 = new BigDecimal (1000);
org.junit.Assert.assertTrue(bd1.compareTo(bd2) == 0); 

回答by Daniele Segato

Comparing BigDecimalwith compareTo()works (as in: it ignore the scale and compare the actual number) but when unit testing it's useful to know what's the actual number, specially when the test fail.

BigDecimalcompareTo()作品比较(例如:它忽略比例并比较实际数字)但是在单元测试时,知道实际数字是多少很有用,特别是当测试失败时。

An option I've used in this case is stripTrailingZeros()on both BigDecimal:

我在这种情况下使用的选项是stripTrailingZeros()两个BigDecimal

assertEquals(new BigDecimal("150").stripTrailingZeros(),
                    otherBigDecimal.stripTrailingZeros());

What this function does is remove zeroes without changing the number, so "150"is converted in "1.5E+2": this way it doesn't matter if you have 150, 150.00or other form in otherBigDecimalbecause they get normalizedinto the same form.

这个函数的作用是在不改变数字的情况下删除零,所以"150"被转换为"1.5E+2":这样,如果你有150150.00或其他形式并不重要,otherBigDecimal因为它们被规范化为相同的形式。

The only difference is a nullin otherBigDecimalwould give a NullPointerExceptioninstead of an assertion error.

唯一的区别是nullinotherBigDecimal会给出一个NullPointerException而不是断言错误。

回答by frhack

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

断言两个 BigDecimal 在 matematically 上相等的官方 junit 解决方案是使用 hamcrest。

With java-hamcrest 2.0.0.0we can use this syntax:

使用java-hamcrest 2.0.0.0,我们可以使用以下语法:

    // 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

Hamcrest 1.3 快速参考

回答by alditis

Other alternative for specific scale and rounded:

特定比例和四舍五入的其他替代方案:

import static org.assertj.core.api.Assertions.assertThat;

...

BigDecimal a = new BigDecimal(100.05);
BigDecimal b = new BigDecimal(100.048);

a = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
b = b.setScale(2, BigDecimal.ROUND_HALF_EVEN);

assertThat(a).isEqualTo(b);

回答by Antonio

Can't you just use a toString() as in :

你不能像这样使用 toString() :

assertEquals("0.02", taxes.toString());

where taxes is a BigDecimal.

其中税是 BigDecimal。