6

In the following example:

 Execution execution = mock(Execution.class); when(execution.getLastQty()).thenReturn(1000.0); when(execution.getLastPrice()).thenReturn(75.0); order.onFillReceived(execution); assertEquals(0, order.getLeavesQty(), 0); 

Execution has many other methods that should not be called. Only the two methods that have been mocked should be used within this test and should be called. If any other methods are called, then the test should fail.

How to I tell Mockito to fail if any other methods are called?

3
  • 1
    Why would you want to? If the Order class happens to call other methods of the Execution class, and it doesn't affect the outcome of the behaviour that you're testing, then why would you want the test to fail? Commented Jan 24, 2012 at 8:03
  • In this particular case ... let's say that order.onFillReceived winds up assigning order.getLeavesQty based on the method from execution.getWrongQty(). Because getWrongQty will return the default value for that method (0), the assert will still work even though getLeavesQty isn't being set by the right value. This is only because the default for wrong values is the same value as what is being asserted in the test. Commented Jan 24, 2012 at 11:53
  • I don't quite follow that. But wouldn't it be cleaner to choose values for the test that can't easily happen by accident? So that if the test passes, you know that the calculation has happened correctly. If the test doesn't demonstrate that the correct value was calculated (rather than happening by accident), I would question the value of the test. Commented Jan 24, 2012 at 20:52

2 Answers 2

9

The documentation covers this explicitly. You want to call verifyNoMoreInteractions, either after calling verify (as per the docs) or

verify(execution).getLastQty(); verify(execution).getLastPrice(); verifyNoMoreInteractions(execution); 

or using ignoreStubs:

verifyNoMoreInteractions(ignoreStubs(execution)); 
Sign up to request clarification or add additional context in comments.

6 Comments

Adding this fails the test complaining that ... No Interactions Wanted Here.
On the flip side ... if ignoreStubs is used (available in 1.9), the above works correctly. The actual code is verifyNoMoreInteractions(ignoreStubs(execution));
@user465342: Well that depends on what you were trying to do. You haven't shown what onFillReceived does - I suspect that if it only calls getLastQty() and getLastPrice() once each, then the above would be fine, I believe. But yes, if you want to treat those as stub methods then ignoreStubs looks like the right way to go.
If you want to use verifyNoMoreInteractions without using ignoreStubs as per @JonSkeet's answer, you'd have to actually put verify calls in for the method calls that you are expecting (presumably getLastQty and getLastPrice once each). Otherwise, the test will fail if these methods have been called. However, I'm not quite brave enough to downvote SO royalty! :-)
@DavidWallace: Thanks for the detail. Will edit the answer. (Can you check that it looks correct now?)
|
0

You could try the never method if that fits the use case:

i.e.

verify(execution, never()).someOtherMethod(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.