且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为方法调用的每个实例返回相同的值

更新时间:2023-01-28 10:55:17

这是不可能的。对于常规的Mockito,你需要在when()调用中使用一些模拟对象,而不是任何匹配器。

This is not possible. With regular Mockito you need some mock object in the when() call, not an any matcher.

对于你的例子,当你说任何(File.class)

For your example, when you say any(File.class)

when(any(File.class).canWrite()).thenReturn(Boolean.FALSE)

您需要将一个文件对象实例化为模拟

You need to have a file object already instantiated as a Mock

File fileMock = mock(File.class);    
when(fileMock.canWrite()).thenReturn(Boolean.FALSE)