然后在mockito的返回中打印语句

use*_*342 4 java testing mockito

在编写我的测试用例时,我正在使用Mockito来模拟某个类.

有没有办法在返回值之前打印一些语句?喜欢:

when(x.callFunction(10).thenReturn(new String("Hello"));
Run Code Online (Sandbox Code Playgroud)

上述声明有效,但我无法执行以下操作:

when(x.callFunction(10).thenReturn({
   System.out.println("Mock called---going to return hello");
   return new String("Hello");});
Run Code Online (Sandbox Code Playgroud)

Rol*_*der 5

随着thenAnswer您可以执行每次调用模拟的方法时附加的动作.

when(x.callFunction(10)).thenAnswer(new Answer<String>() {
    public String answer(InvocationOnMock invocation)  {
        System.out.println("Mock called---going to return hello");
        return "Hello";
    }
});
Run Code Online (Sandbox Code Playgroud)

另见thenAnswer Vs然后返回.