Java 在Mockito中,多重返回是如何工作的?
我使用Mockito有一行代码:Java 在Mockito中,多重返回是如何工作的?,java,mockito,testcase,Java,Mockito,Testcase,我使用Mockito有一行代码: when(mock.method()).thenReturn(foo).thenReturn(bar).thenThrow(新异常(“测试”); 上述声明是否与: when(mock.method()).thenReturn(foo)thenThrow(newexception(“test”); when(mock.method()).thenReturn(bar).thenThrow(新异常(“测试”)); 请任何人解释一下,如何执行thenReturn?
when(mock.method()).thenReturn(foo).thenReturn(bar).thenThrow(新异常(“测试”);
上述声明是否与:
when(mock.method()).thenReturn(foo)thenThrow(newexception(“test”);
when(mock.method()).thenReturn(bar).thenThrow(新异常(“测试”));
请任何人解释一下,如何执行thenReturn
?
它是如何工作的?快速浏览一下Mockito的文章就可以找到答案
//you can set different behavior for consecutive method calls.
//Last stubbing (e.g: thenReturn("foo")) determines the behavior of further consecutive calls.
when(mock.someMethod("some arg"))
.thenThrow(new RuntimeException())
.thenReturn("foo");
//Alternative, shorter version for consecutive stubbing:
when(mock.someMethod("some arg"))
.thenReturn("one", "two");
//is the same as:
when(mock.someMethod("some arg"))
.thenReturn("one")
.thenReturn("two");
基本上,多次调用然后返回将定义该方法在多次调用中的行为。您是否试图找到答案?