How do I handle unmatched parameters in Mockito?
java, junit, mockito
Solution
(Slight disclaimer, I've never done this personally, just read about it in the javadoc)... If all of your methods on your mock interface would be ok with the same default behaviour, you could set the default answer on your mock in a manner like:
Foo myMock = Mockito.mock(Foo.class,new ThrowsExceptionClass(IllegalArgumentException.class));
Mockito.when(myMock.doSomething(Matchers.eq("1"))).thenReturn("1");
JavaDoc Links for: Mockito#mock and ThrowsExceptionClass
Alternatively, as is discussed in the Stubbing tutorial, order of the stubbing matters and last matching wins, so you might be able to also do:
Foo myMock = Mockito.mock(Foo.class);
Mockito.when(myMock.doSomething(Matchers.any(String.class))).thenThrow(IllegalArgumentException.class);
Mockito.when(myMock.doSomething(Matchers.eq("1"))).thenReturn("1");
Problem
I like to do something like the following: ``` .when( myMock.doSomething( Matchers.eq( "1" ) ) ) .thenReturn( "1" ) .othwerwise() .thenThrow( new IllegalArgumentException() ); ``` Of course `otherwise()` method doesn't exist but just to show you what I want to accomplish.