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
JUnit Assert with BigDecimal
提问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
assertSame
tests 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
AssertionError
without a message is thrown.
断言两个对象引用同一个对象。如果它们不相同,
AssertionError
则抛出一个不带消息的消息。
In your case, since bd1
and bd2
are both new BigDecimal
, the objects aren't the same, hence the exception.
在您的情况下,由于bd1
和bd2
都是 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
AssertionError
without a message is thrown. If expected and actual arenull
, they are considered equal.
断言两个对象相等。如果不是,
AssertionError
则抛出一个不带消息的消息。如果预期和实际是null
,则认为它们相等。
BigDecimal bd1 = new BigDecimal (1000);
BigDecimal bd2 = new BigDecimal (1000);
org.junit.Assert.assertEquals(bd1,bd2);
回答by Maroun
bd1
and bd2
are two differentobjects, and since assertSame
checks the object referenceusing the ==
operator, you're getting that message, see the docs:
bd1
andbd2
是两个不同的对象,并且由于使用运算符assertSame
检查对象引用==
,因此您会收到该消息,请参阅文档:
Asserts that two objects refer to the same object. If they are not the same, an
AssertionError
without a message is thrown.
断言两个对象引用同一个对象。如果它们不相同,
AssertionError
则抛出一个不带消息的消息。
You should use assertEquals
instead, it checks that the two objects are equal - which is what you want.
您应该改用assertEquals
它,它会检查两个对象是否相等 - 这就是您想要的。
Note that comparing two BigDecimal
objects using the ==
operator will work as long as their values are cached (for 0 through 10) values.
请注意,只要它们的值被缓存(0 到 10)值,就可以BigDecimal
使用==
运算符比较两个对象。
回答by Vikrant Kashyap
Use AssertEquals
instead of AssertSame
... reason because assertequals
checks the value but assertsame
checks the refrence..
使用AssertEquals
而不是AssertSame
...原因因为assertequals
检查值但assertsame
检查参考..
回答by Hoopje
The method assertSame
tests 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 equals
method) on BigDecimal
s. For example, new BigDecimal("100").divide(new BigDecimal("10.0")).equals(new BigDecimal("10"))
evaluates to false
because equals
also looks at the scale of the BigDecimal
instances.
但是,在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 BigDecimal
s by using the compareTo
method:
在许多情况下,最好BigDecimal
使用以下compareTo
方法比较s :
assertTrue(bd1.compareTo(bd2) == 0);
回答by Endery
assertSame
checks if both objects are the same instance. assertEquals
checks 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 BigDecimal
with 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.
BigDecimal
与compareTo()
作品比较(例如:它忽略比例并比较实际数字)但是在单元测试时,知道实际数字是多少很有用,特别是当测试失败时。
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.00
or other form in otherBigDecimal
because they get normalizedinto the same form.
这个函数的作用是在不改变数字的情况下删除零,所以"150"
被转换为"1.5E+2"
:这样,如果你有150
,150.00
或其他形式并不重要,otherBigDecimal
因为它们被规范化为相同的形式。
The only difference is a null
in otherBigDecimal
would give a NullPointerException
instead of an assertion error.
唯一的区别是null
inotherBigDecimal
会给出一个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));
回答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。