gpt4 book ai didi

java - 我如何比较 BigDecimals 以使我的测试通过?

转载 作者:搜寻专家 更新时间:2023-11-01 00:59:44 24 4
gpt4 key购买 nike

<分区>

我在 JUnit 测试中遇到了以下相同的奇怪情况。

所以我有这个测试方法:

@Test
public void getNavInfoTest() throws ParseException {

TirAliquotaRamoI expectedObject = new TirAliquotaRamoI();

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date;

date = formatter.parse("2015-08-01");

Date dataInizio = formatter.parse("2015-08-01");
Date dataFine = formatter.parse("2100-12-31");

expectedObject.setDataElaborazione(date);
expectedObject.setTassoLordoAnnuoAppl(BigDecimal.ZERO);
expectedObject.setTassoGiornalieroNetto(BigDecimal.ZERO);
expectedObject.setAliquota(BigDecimal.ONE);
expectedObject.setDataInizio(dataInizio);
expectedObject.setDataFine(dataFine);

TirAliquotaRamoI tirAliquotaRamoI = pucManager.getNavInfo(date);

assertEquals(tirAliquotaRamoI.getAliquota(), expectedObject.getAliquota());

}

最后,我只是简单地测试 tirAliquotaRamoI.getAliquota()(从数据库查询中获得)是否具有为创建的 expectedObject 定义的相同字段的相同值>:

assertEquals(tirAliquotaRamoI.getAliquota(), expectedObject.getAliquota());

所以预期对象的字段是使用 BigDecimal.ONE 常量创建的,使用调试器我可以看到它的值为 1

tirAliquotaRamoI.getAliquota()得到的值为1.000000000

因此,理论上,两者都代表相同的值 1,但测试失败,我得到:

java.lang.AssertionError: expected:<1.000000000> but was:<1>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.fideuram.dbmanager.PucManagerTest.getNavInfoTest(PucManagerTest.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

为什么两者都代表相同的 1.0 值?我该如何解决这个问题才能通过测试?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com