Mockito - verify a double value
java, mockito, unit-testing
Solution
You could capture the value, e.g.
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, e.g.:
verify(myManager).method1(AdditionalMatchers.eq(expected, delta));
Although use `AdditonalMatchers` matchers wisely, as per the documentation:
AdditionalMatchers provides rarely used matchers, kept only for somewhat compatibility with 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.
Problem
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. The verification fails. It expects 3953.28 but 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. Thanks