Java mockito拒绝将TypeSafeMatcher与泛型方法API配对
我想测试调用某些API的代码:Java mockito拒绝将TypeSafeMatcher与泛型方法API配对,java,unit-testing,java-8,mocking,mockito,Java,Unit Testing,Java 8,Mocking,Mockito,我想测试调用某些API的代码: public <T extends MessageLite> ApiFuture<String> publish(final T message) throws Exception { } public <T extends MessageLite> ApiFuture<String> publish(final T message, final ApiFutureCallback<T> future
public <T extends MessageLite> ApiFuture<String> publish(final T message) throws Exception {
}
public <T extends MessageLite> ApiFuture<String> publish(final T message, final ApiFutureCallback<T> futureCallback) throws Exception {
}
public <T> String publishSynchronous(final String message, final ApiFutureCallback<T> futureCallback) throws Exception {
}
而这个配对者:
public class FailResultMatcher extends TypeSafeMatcher<MessageLite> {
@Override
protected boolean matchesSafely(final MessageLite sentResult) {
return !((SdkResult)sentResult).getIsSuccess();
}
public static FailResultMatcher isFailureResult() {
return new FailResultMatcher();
}
@Override
public void describeTo(final Description description) {
}
}
但我在测试编译中遇到了一个错误:
Error:(131, 42) java: no suitable method found for publish(com.w.sdkService.matchers.FailResultMatcher,java.lang.Object)
method linqmap.cloud.google.pubsub.CustomPublisher.<T>publish(java.lang.String,com.google.api.core.ApiFutureCallback<T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; com.w.sdkService.matchers.FailResultMatcher cannot be converted to java.lang.String))
method linqmap.cloud.google.pubsub.CustomPublisher.<T>publish(T) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
method linqmap.cloud.google.pubsub.CustomPublisher.<T>publish(T,com.google.api.core.ApiFutureCallback<T>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: com.w.sdkService.matchers.FailResultMatcher
upper bound(s): com.google.protobuf.MessageLite)
如何修复此问题?根据文档,您必须使用argThatmatcher,这是一种Mockito匹配器,允许您使用自定义参数匹配器:
//存根
当检查时,给我一个新的Myham CrestMatcher;
//核实
验证mock.giveMeargThatnew MyHamcrestMatcher;
您没有说您使用的是Mockito 1还是Mockito 2,但想法类似,只是静态导入不同:
1:导入静态org.mockito.Matchers.argThat;或者为了简单起见,org.mockito.mockito扩展了匹配器
2:导入静态org.mockito.hamcrest.MockitoHamcrest.argThat;
额外提示,为了可读性,您可以将times0替换为,因此在您的情况下:
验证CustomPublisher,never.PublishArg是FailureResult,anyObject;
根据文档和,您必须使用argThatmatcher,这是一种Mockito匹配器,允许您使用自定义参数匹配器:
//存根
当检查时,给我一个新的Myham CrestMatcher;
//核实
验证mock.giveMeargThatnew MyHamcrestMatcher;
您没有说您使用的是Mockito 1还是Mockito 2,但想法类似,只是静态导入不同:
1:导入静态org.mockito.Matchers.argThat;或者为了简单起见,org.mockito.mockito扩展了匹配器
2: 导入static org.mockito.hamcrest.MockitoHamcrest.arga;
额外提示,为了可读性,您可以将times0替换为,因此在您的情况下:
验证CustomPublisher,never.PublishArg是FailureResult,anyObject;
寻求调试帮助的问题此代码为什么不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参见:如何创建。使用编辑链接改进您的问题-不要通过评论添加更多信息。谢谢不要太多地解释代码是如何组合在一起的。把它放在一起,提供一个!寻求调试帮助的问题此代码为什么不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参见:如何创建。使用编辑链接改进您的问题-不要通过评论添加更多信息。谢谢不要太多地解释代码是如何组合在一起的。把它放在一起,提供一个!
Error:(131, 42) java: no suitable method found for publish(com.w.sdkService.matchers.FailResultMatcher,java.lang.Object)
method linqmap.cloud.google.pubsub.CustomPublisher.<T>publish(java.lang.String,com.google.api.core.ApiFutureCallback<T>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; com.w.sdkService.matchers.FailResultMatcher cannot be converted to java.lang.String))
method linqmap.cloud.google.pubsub.CustomPublisher.<T>publish(T) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
method linqmap.cloud.google.pubsub.CustomPublisher.<T>publish(T,com.google.api.core.ApiFutureCallback<T>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: com.w.sdkService.matchers.FailResultMatcher
upper bound(s): com.google.protobuf.MessageLite)