Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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_Unit Testing_Mocking_Mockito_Powermock - Fatal编程技术网

Java 使用链连接来连接Mockito?

Java 使用链连接来连接Mockito?,java,unit-testing,mocking,mockito,powermock,Java,Unit Testing,Mocking,Mockito,Powermock,在Mockito中,当我们试图模拟方法调用时,假设我们配置了如下内容 when(exampleClass.getOutputString(anyString())).thenReturn("output1"); when(exampleClass.getOutputString(anyString())).thenReturn("output1").thenReturn("output2"); 这是可以理解的。但是我的问题是,这个thenReturn(“output1”)方法返回一个ongo

在Mockito中,当我们试图模拟方法调用时,假设我们配置了如下内容

when(exampleClass.getOutputString(anyString())).thenReturn("output1");
when(exampleClass.getOutputString(anyString())).thenReturn("output1").thenReturn("output2");
这是可以理解的。但是我的问题是,这个
thenReturn(“output1”)
方法返回一个ongoingstrubing对象的原因是什么(与
when(exampleClass.getOutputString(anyString())
方法返回的内容相同),所以我们可以这样做

when(exampleClass.getOutputString(anyString())).thenReturn("output1");
when(exampleClass.getOutputString(anyString())).thenReturn("output1").thenReturn("output2");


然而,在上述两种情况下,当使用mock时,它只返回
“output1”
,仅此而已。任何人都知道,为什么会出现这种链接功能,以及它的用途是什么?同样的情况也适用于
doReturn()

这意味着在第一次调用中给出
输出1
,在第二次调用中给出
输出2
,依此类推

when(exampleClass.getOutputString(anyString())).thenReturn("output1").thenReturn("output2");

,您需要达到目标测试类中的一个条件的第二个条件

output1
对于第一种情况,但对于第二种情况,您希望测试失败

试试catch子句,或者看看代码是否覆盖了异常等不完整的场景


这意味着在第一次调用中给出
output1
,在第二次调用中给出
output2
,依此类推

when(exampleClass.getOutputString(anyString())).thenReturn("output1").thenReturn("output2");

,您需要达到目标测试类中的一个条件的第二个条件

output1
对于第一种情况,但对于第二种情况,您希望测试失败

试试catch子句,或者看看代码是否覆盖了异常等不完整的场景