如何调用自定义 hamcrest 匹配器?
how can I call a custom hamcrest matcher?
我想检查何时使用 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);
这是编译错误:
这是您可以继续的方式
classTimeMatcher
,你只需要LocalDateTime
public class TimeMatcher {
public static Matcher<RealtimeUpdate> isTimeEqual(final LocalDateTime localDateTime) {
return new BaseMatcher<RealtimeUpdate>() {
@Override
public void describeTo(final Description description) {
description.appendText("Date doesn't match with "+ localDateTime);
}
@Override
public boolean matches(final Object item) {
final RealtimeUpdate realtimeUpdate = (RealtimeUpdate) item;
return realtimeUpdate.currentTime.isEqual(localDateTime);
}
};
}
}
测试:
Mockito.verify(mockRoutingServerApi).sendRealTimeUpdate(
new ThreadSafeMockingProgress().getArgumentMatcherStorage()
.reportMatcher(TimeMatcher.isTimeEqual(localDateTime2))
.returnFor(RealtimeUpdate.class));
您需要使用 returnFor
来提供参数类型,即 sendRealTimeUpdate
预期的 RealtimeUpdate
这相当于:
Mockito.verify(mockRoutingServerApi).sendRealTimeUpdate(
Matchers.argThat(TimeMatcher.isTimeEqual(localDateTime2))
);
我想检查何时使用 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);
这是编译错误:
这是您可以继续的方式
classTimeMatcher
,你只需要LocalDateTime
public class TimeMatcher {
public static Matcher<RealtimeUpdate> isTimeEqual(final LocalDateTime localDateTime) {
return new BaseMatcher<RealtimeUpdate>() {
@Override
public void describeTo(final Description description) {
description.appendText("Date doesn't match with "+ localDateTime);
}
@Override
public boolean matches(final Object item) {
final RealtimeUpdate realtimeUpdate = (RealtimeUpdate) item;
return realtimeUpdate.currentTime.isEqual(localDateTime);
}
};
}
}
测试:
Mockito.verify(mockRoutingServerApi).sendRealTimeUpdate(
new ThreadSafeMockingProgress().getArgumentMatcherStorage()
.reportMatcher(TimeMatcher.isTimeEqual(localDateTime2))
.returnFor(RealtimeUpdate.class));
您需要使用 returnFor
来提供参数类型,即 sendRealTimeUpdate
RealtimeUpdate
这相当于:
Mockito.verify(mockRoutingServerApi).sendRealTimeUpdate(
Matchers.argThat(TimeMatcher.isTimeEqual(localDateTime2))
);