java 如何处理 Mockito 中不匹配的参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13061241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I handle unmatched parameters in Mockito?
提问by supertonsky
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.
当然otherwise()
方法不存在,只是为了向你展示我想要完成的事情。
采纳答案by Charlie
(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 mockin a manner like:
(轻微的免责声明,我从来没有亲自做过这件事,只是在 javadoc 中阅读了它)......如果你的模拟界面上的所有方法都可以使用相同的默认行为,你可以在你的模拟上设置默认答案以如下方式:
Foo myMock = Mockito.mock(Foo.class,new ThrowsExceptionClass(IllegalArgumentException.class));
Mockito.when(myMock.doSomething(Matchers.eq("1"))).thenReturn("1");
JavaDoc Links for: Mockito#mockand ThrowsExceptionClass
JavaDoc的链接:的Mockito#模拟和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");
回答by Matt
you could create your own Answer implementation which would pay attention to the called parameters:
您可以创建自己的 Answer 实现,它会关注被调用的参数:
myMock.doSomething(Mockito.any(String.class)).thenAnswer( myAnswer );
The implementation of said answer could do something like this:
上述答案的实现可以做这样的事情:
public String answer(InvocationOnMock invocation) {
if ("1".equals(invocation.getArguments()[0])) {
return "1";
}
else {
throw new IllegalArgumentException();
}
}
回答by Yogendra Singh
Just use opposite condition i.e. consider your example itself. You may want to use not(eq())
when you need otherwise
:
只需使用相反的条件,即考虑您的示例本身。您可能需要not(eq())
在需要时使用otherwise
:
.when( myMock.doSomething(Matchers.eq( "1" )))
.thenReturn( "1" )
.when( myMock.doSomething(not(Matchers.eq( "1" ))))
.thenThrow( new IllegalArgumentException() );
回答by Thermech
With java 8 lambda you can do:
使用 java 8 lambda,您可以执行以下操作:
myMock.doSomething(Mockito.any(String.class)).thenAnswer(invocation -> {
Object arg = invocation.getArguments()[0];
if ("1".equals(arg)) {
return "1";
}
throw new IllegalArgumentException("Expected 1 but got " + arg);
});
回答by Dmitry P.
The way described by the accepted answer by @Charlie doesn't work (anymore). When you try to override the general throw exception behaviour for some argument the first rule is triggered and you have an exception (just as you asked).
@Charlie 接受的答案所描述的方式(不再)。当您尝试覆盖某个参数的一般抛出异常行为时,第一个规则被触发并且您有一个异常(正如您所要求的)。
Mockito.when(myMock.doSomething(any()))
.thenThrow(IllegalArgumentException.class);
Mockito.when(myMock.doSomething(eq("1"))).thenReturn("1"); //An exception is thrown here
// because of the call to .doSomething() on the mock object
To avoid that call one can use Mockito.doReturn()
method:
为了避免该调用,可以使用Mockito.doReturn()
方法:
Mockito.when(myMock.doSomething(any()))
.thenThrow(IllegalArgumentException.class);
Mockito.doReturn("1").when(myMock).doSomething(eq("1"));
The initial issue is one of reasons why doReturn()
exists according to it's javadoc:
最初的问题是doReturn()
根据它的 javadoc 存在的原因之一:
Here are those rare occasions when doReturn() comes handy:
<...some lines are skipped...>
Overriding a previous exception-stubbing:
hen(mock.foo()).thenThrow(new RuntimeException());
//Impossible: the exception-stubbed foo() method is called so RuntimeException is thrown.
when(mock.foo()).thenReturn("bar");
//You have to use doReturn() for stubbing:
doReturn("bar").when(mock).foo();
回答by fairidox
Alternatively you can use verify, as follows:
或者,您可以使用验证,如下所示:
when(myMock.doSomething("1")).thenReturn( "1" );
assertEquals(myMock.doSomething("1"),"1");
verify(myMock).doSomething("1")