I have an interface Foo
with method int Foo.bar(int)
that I want to mock with Mockito. I want the mocked method to return 99
if I pass in 1
, but all other values will throw an exception. Can I do this?
final Foo foo = mock(Foo.class);
when(foo.bar(1)).thenReturn(99);
when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
In other words, will the 1
take precedence over anyInt()
? I wouldn't want it throwing an exception for 1
. The docs say that for multiple definitions the last definition is more important, but I couldn't tell if that meant for identical arguments or not. If it applies here, would I need to define the wildcard anyInt()
first? Or do the two even have any relation, as one of them is a matcher and the other is just a value?
Matcher methods can't be used as return values; there is no way to phrase thenReturn(anyInt()) or thenReturn(any(Foo. class)) in Mockito, for instance. Mockito needs to know exactly which instance to return in stubbing calls, and will not choose an arbitrary return value for you.
To solve your problem of knowing whether the setName() method was called in your code with the specified value use the Mockito verify method. For example: verify(mockedFoo, times(1)). setName("test");
@InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock annotations into this instance. @Mock is used to create mocks that are needed to support the testing of the class to be tested. @InjectMocks is used to create class instances that need to be tested in the test class.
A stub is a fake class that comes with preprogrammed return values. It's injected into the class under test to give you absolute control over what's being tested as input. A typical stub is a database connection that allows you to mimic any scenario without having a real database.
You have two options: Matching "any value but one", and overriding stubbing. (I suppose you could also use an Answer for complex custom behavior, but that's overkill for situations like this one.)
Mockito's AdditionalMatchers
class offers a number of useful matchers, including operators such as not
. This would allow you to set behavior for all values except for a specific value (or expression).
when(foo.bar(1)).thenReturn(99);
when(foo.bar(not(eq(1)))).thenThrow(new IllegalArgumentException());
Be careful to note that operators must be used with matchers instead of values, possibly requiring Matchers.eq
as an explicit equals
matcher, due to Mockito's argument matcher stack:
/* BAD */ when(foo.bar(not( 1 ))).thenThrow(new IllegalArgumentException());
/* GOOD */ when(foo.bar(not(eq(1)))).thenThrow(new IllegalArgumentException());
For stubbing, the last-defined matching chain wins. This allows you to set up general test fixture behavior in a @Before
method and override it in individual test cases if you wish, but also implies that order matters in your stubbing calls.
when(foo.baz(anyInt())).thenReturn("A", "B"); /* or .thenReturn("A").thenReturn("B"); */
when(foo.baz(9)).thenReturn("X", "Y");
foo.baz(6); /* "A" because anyInt() is the last-defined chain */
foo.baz(7); /* "B" as the next return value of the first chain */
foo.baz(8); /* "B" as Mockito repeats the final chain action forever */
foo.baz(9); /* "X" because the second chain matches for the value 9 */
foo.baz(9); /* "Y" forever because the second chain action still matches */
Consequently, you should never see the two stubs in the order listed in the question, because if a general match immediately follows a specific match then the specific match will never be used (and may as well be deleted).
Beware that you'll sometimes need to change syntax to doAnswer
when overriding spies or dangerous stubbed behavior. Mockito knows not to count calls to when
for verification or for advancing along thenVerb
chains, but exceptions could still cause your test to fail.
/* BAD: the call to foo.bar(1) will throw before Mockito has a chance to stub it! */
when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
when(foo.bar(1)).thenReturn(99);
/* GOOD: Mockito has a chance to deactivate behavior during stubbing. */
when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
doReturn(99).when(foo).bar(1);
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