0
在Mockito中,我該如何模擬一個將函數接口作爲方法參數的方法?例如:如何使用功能接口參數模擬方法?
String foo(String m, Function<Double, Options> r) {}
在Mockito中,我該如何模擬一個將函數接口作爲方法參數的方法?例如:如何使用功能接口參數模擬方法?
String foo(String m, Function<Double, Options> r) {}
下面一直沒有編譯測試/運行,可能有小錯誤。我知道它有通用的警告
class Foo {
String foo(String m, Function<Double, Options> r) {}
}
class Bar {
Foo myFoo; // assume this is normally injected or something
boolean works() {
// ... code that somehow uses myFoo and processes the String returned
}
}
class BarTestCase {
@Mock private Foo foo;
@InjectMocks private Bar bar
@Test
public void testFooMethod() {
when(foo.foo(anyString(), any(Function.class)).thenReturn("ABCD1234");
assertTrue(bar.works());
}
}
如果您在模擬對象上調用void方法,根據定義,它什麼都不做,因爲它什麼都不返回。 – Compass
如果它返回一個值,你會怎麼做? – Bharathi
期望any()對象並返回任何你想要的。 – Compass