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
Static import could be used in our code, but otherwise is done to underscore the methods of Mockito. In the aforementioned case the test double is a stub, that is the object is instructed to return some value when a particular method is called and it provides some input to our system under test.
Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method. We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified.
Mockito Argument Matcher - eq() When we use argument matchers, then all the arguments should use matchers. If we want to use a specific value for an argument, then we can use eq() method.
verifyNoMoreInteractions() public static void verifyNoMoreInteractions(Object... mocks) Checks if any of given mocks has any unverified interaction. We can use this method after calling verify() methods.
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;
}
}));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With