如何使用功能接口参数模拟方法?

时间:2017-07-18 18:44:59

标签: java unit-testing mockito

在Mockito中,我如何模拟一个将功能接口作为方法参数的方法?例如:

library(ggplot2) data<-read.csv("pt1.csv", header=T, sep="\t", dec=",") data$time<-as.Date(data$time, "%d/%m/%y", origin="1970-01-01") pt1.plot<-<-ggplot(data, aes(time, ert, group=1, na.rm=T))+geom_rect(aes(xmin=2, xmax=Inf, ymin=-Inf, ymax=Inf), fill="lightgreen", alpha=0.03)+geom_line()+labs(x="", y="ert")+geom_hline(aes(yintercept=0.5), colour="#990000", linetype="dashed") pt1.plot

1 个答案:

答案 0 :(得分:1)

以下未经过编译测试/运行,可能会有轻微错误。我知道它有通用

的警告
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());
    }
}