java如何调用自定义hamcrest匹配器?
我想检查何时使用realtimeUpdate
调用模拟,其中currentTime
字段等于一些LocalDateTime
:
我想使用自定义匹配器运行这样的代码:
verify(mockServerApi).sendUpdate(new TimeMatcher().isTimeEqual(update, localDateTime2));
但是,当我尝试使用此自定义匹配器运行时,出现编译错误
我怎样才能解决这个问题
public class TimeMatcher {
public Matcher<RealtimeUpdate> isTimeEqual(RealtimeUpdate realtimeUpdate, final LocalDateTime localDateTime) {
return new BaseMatcher<RealtimeUpdate>() {
@Override
public boolean matches(final Object item) {
final RealtimeUpdate realtimeUpdate = (RealtimeUpdate) item;
return realtimeUpdate.currentTime.equalTo(localDateTime);
}
这是方法签名
void sendRealTimeUpdate(RealtimeUpdate realtimeUpdate);
这就是编译错误:
# 1 楼答案
以下是您可以继续操作的方法
类
TimeMatcher
,您只需要LocalDateTime
测试:
您需要使用
returnFor
来提供RealtimeUpdate
的参数类型,正如sendRealTimeUpdate
所期望的那样这相当于: