gpt4 book ai didi

java - 在 mockito 的 thenReturn 中打印语句

转载 作者:行者123 更新时间:2023-11-28 19:51:05 24 4
gpt4 key购买 nike

我在编写测试用例时使用 Mockito 来模拟某个类。

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

when(x.callFunction(10).thenReturn(new String("Hello"));

上面的语句有效,但是我无法执行以下操作:

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

最佳答案

使用 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";
}
});

另见 thenAnswer Vs thenReturn .

关于java - 在 mockito 的 thenReturn 中打印语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com