Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Mockito中,多重返回是如何工作的?_Java_Mockito_Testcase - Fatal编程技术网

Java 在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?

我使用Mockito有一行代码:

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");

基本上,多次调用然后返回将定义该方法在多次调用中的行为。

您是否试图找到答案?