如何处理Mockito的"任何其他价值"?

Gar*_*son 23 java mockito

我有一个Foo方法接口int Foo.bar(int),我想用Mockito模拟.99如果我传入,我希望模拟的方法返回1,但所有其他值将抛出异常.我可以这样做吗?

final Foo foo = mock(Foo.class);
when(foo.bar(1)).thenReturn(99);
when(foo.bar(anyInt())).thenThrow(new IllegalArgumentException());
Run Code Online (Sandbox Code Playgroud)

换句话说,1优先考虑anyInt()?我不希望它抛出异常1.该文档说,对于多个定义,最后的定义是比较重要的,但我不能告诉这是否意味着对于相同的参数或没有.如果它适用于此,我是否需要先定义通配符anyInt()?或者两者甚至有任何关系,因为其中一个是匹配器而另一个只是一个值?

Jef*_*ica 25

你有两个选择:匹配"任何值,但一个",并覆盖存根.(我想你也可以使用一个复杂的自定义行为的答案,但这对于像这样的情况来说太过分了.)

存储除给定值之外的任何值

Mockito的AdditionalMatchers课程提供了许多有用的匹配器,包括运营商not.这将允许您为除特定值(或表达式)之外的所有值设置行为.

when(foo.bar(1)).thenReturn(99);
when(foo.bar(not(eq(1)))).thenThrow(new IllegalArgumentException());
Run Code Online (Sandbox Code Playgroud)

请注意,由于Mockito的参数匹配器堆栈,操作符必须与匹配器一起使用而不是值,可能需要Matchers.eq作为显式equals匹配:

/* BAD */  when(foo.bar(not(  1  ))).thenThrow(new IllegalArgumentException());
/* GOOD */ when(foo.bar(not(eq(1)))).thenThrow(new IllegalArgumentException());
Run Code Online (Sandbox Code Playgroud)

覆盖存根

对于存根,最后定义的匹配链获胜.这允许您在方法中设置常规测试夹具行为,@Before并在您希望的情况下在单个测试用例中覆盖它,但也暗示订单在您的存根调用中很重要.

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 */
Run Code Online (Sandbox Code Playgroud)

因此,您永远不应该按照问题中列出的顺序看到两个存根,因为如果一般匹配紧跟在特定匹配之后,则永远不会使用特定匹配(也可以删除).

请注意,doAnswer在覆盖间谍或危险的存根行为时,有时需要更改语法.知道的Mockito不数到电话when进行验证或沿前进thenVerb链,但例外仍然会造成你的测试失败.

/* 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);
Run Code Online (Sandbox Code Playgroud)