Java 使用链连接来连接Mockito?
在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
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子句,或者看看代码是否覆盖了异常等不完整的场景