简体   繁体   English

Mockito - 验证双倍值

[英]Mockito - verify a double value

I have a method called method1 that takes a double which is called on myManager I am passing into this 65.888 * 60. When I try and verify this I get floating point problems. 我有一个名为method1的方法,它接受一个在myManager上调用的double我传入这个65.888 * 60.当我尝试验证这个时,我得到了浮点问题。 The verification fails. 验证失败。 It expects 3953.28 but 3953.280029296875 它预计3953.28但3953.280029296875

verify(myManager, times(1)).method1(65.888 * 60d);

Is there anyway I can make this verify do a fuzzy check for floating point checking. 无论如何我可以使这个验证对浮点检查进行模糊检查。 Much like you do with assertEquals where you input a delta at the end. 就像你在assertEquals中做的那样,你在最后输入一个delta。

Thanks 谢谢

You could capture the value, eg 你可以捕获这个值,例如

final ArgumentCaptor<Double> captor = ArgumentCaptor.forClass(Double.class)
...
verify(myManager).method1(captor.capture());

Then assert: 断言:

assertEquals(expected, captor.getValue(), delta)

Or, perhaps, use an argument matcher which does the assertion: 或者,或许,使用参数匹配器来执行断言:

verify(myManager).method1(doubleThat(new ArgumentMatcher<Double>() 
{
    @Override 
    public boolean matches(final Object actual)
    {
        return Math.abs(expected - (Double) actual) <= delta;
    }
}));

Update: 更新:

Instead of using either of the methods above, you could use AdditionalMatchers.eq(double, double) instead, eg: 您可以使用AdditionalMatchers.eq(double, double)代替上述任何一种方法,例如:

verify(myManager).method1(AdditionalMatchers.eq(expected, delta));

Although use AdditonalMatchers matchers wisely, as per the documentation: 虽然使用AdditonalMatchers明智地匹配,根据文档:

AdditionalMatchers provides rarely used matchers, kept only for somewhat compatibility with EasyMock. AdditionalMatchers提供很少使用的匹配器,仅与EasyMock保持一定的兼容性。 Use additional matchers very judiciously because they may impact readability of a test. 非常明智地使用其他匹配器,因为它们可能会影响测试的可读性。 It is recommended to use matchers from Matchers and keep stubbing and verification simple. 建议使用Matchers的匹配器,并保持简单和验证。

There is a Hamcrest matcher that is perfect for this. Hamcrest匹配器非常适合这种情况。

org.hamcrest.Matchers.closeTo(value, error)

So you could write something like 所以你可以写一些像

verify(myManager).method1(doubleThat(org.hamcrest.Matchers.closeTo(65.888 * 60, 0.001)));

Incidentally, you don't ever need to write times(1) in a Mockito verify , as this is the default type of verify that Mockito gives you. 顺便说一句,您不需要在Mockito verify写入times(1) ,因为这是Mockito为您提供的默认verify类型。

Following code works for me: 以下代码适用于我:

private class MockedClass {
    public void method1(double d) {}
}

@Test
public final void testMockito() {
    MockedClass d = mock(MockedClass.class);
    d.method1(3953.28);
    verify(d, times(1)).method1(65.888 * 60d);
}

Maybe you should instead call the method with anyDouble() or use following Matcher: http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/number/IsCloseTo.html 也许您应该使用anyDouble()调用该方法或使用以下匹配器: http ://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/number/IsCloseTo.html

What about using: 怎么样使用:

when(65.888).thenReturn(65.888 * 60);

assertEquals(65.888 * 60, new BigDecimal(Double.toString(65.888 * 60))
    .setScale(pRoundingDecimalPlaces, 2).doubleValue());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM