我正在编写一个使用Groovy Spock
进行测试的Java
应用程序。在控制器中,我想测试在实用程序函数中发生的事情。
实用程序函数接受一个String
和2个回调(Consumer
)。
模拟实用程序函数,我如何执行其中一个回调?
public void authenticate(String token, Consumer<User> success, Consumer<Throwable> failure)
Groovy def "..."(){
given:
TokenHandler th = Mock()
// execute the failure callback
th.authenticate(_) >> { token, success, failure -> failure.accept() }
}
模拟的初始化错误,请参见以下初始化:
Consumer<User> successConsumer = new Consumer<User>()
Consumer<Throwable> failureConsumer = new Consumer<Throwable>()
TokenHandler tokenHandlerMock = Mock(TokenHandler){
th.authenticate(_) >> [token, successConsumer, failureConsumer]
}