Generics JMock with(instanceOf(Integer.class))不能在Java8中编译
升级到Java8之后。我现在有以下类型的编译错误:Generics JMock with(instanceOf(Integer.class))不能在Java8中编译,generics,migration,java-8,hamcrest,jmock,Generics,Migration,Java 8,Hamcrest,Jmock,升级到Java8之后。我现在有以下类型的编译错误: The method with(Matcher<Object>) is ambiguous for the type new Expectations(){} 从instanceOf()返回的with似乎与with()所期望的不明确,反之亦然。有没有办法解决这个问题?有一些简单的方法可以帮助编译器 将匹配器分配给局部变量: public static void main(String[] args) { Expectati
The method with(Matcher<Object>) is ambiguous for the type new Expectations(){}
从
instanceOf()
返回的with似乎与with()
所期望的不明确,反之亦然。有没有办法解决这个问题?有一些简单的方法可以帮助编译器
将匹配器分配给局部变量:
public static void main(String[] args) {
Expectations expectations = new Expectations();
Matcher<Integer> instanceOf = Matchers.instanceOf(Integer.class);
expectations.with(instanceOf);
}
publicstaticvoidmain(字符串[]args){
期望值=新的期望值();
Matcher instanceOf=Matchers.instanceOf(Integer.class);
期望值。使用(instanceOf);
}
可以使用类型见证指定类型参数,如下所示:
public static void main(String[] args) {
Expectations expectations = new Expectations();
expectations.with(Matchers.<Integer>instanceOf(Integer.class));
}
publicstaticvoidmain(字符串[]args){
期望值=新的期望值();
expections.with(Matchers.instanceOf(Integer.class));
}
用您自己的类型安全方法包装instanceOf:
public static void main(String[] args) {
Expectations expectations = new Expectations();
expectations.with(instanceOf(Integer.class));
}
public static <T> Matcher<T> instanceOf(Class<T> type) {
return Matchers.instanceOf(type);
}
publicstaticvoidmain(字符串[]args){
期望值=新的期望值();
expections.with(instanceOf(Integer.class));
}
公共静态匹配器instanceOf(类类型){
返回Matchers.instanceOf(类型);
}
我更喜欢上一个解决方案,因为它是可重用的,而且测试仍然易于阅读。确切的JDK版本?JMock版本?一个完整的源代码示例会很有帮助。您可能可以使用(org.hamcrest.Matchers.instanceof(Integer.class))将类型添加到
instanceof
:expections.with(org.hamcrest.Matchers.instanceof(Integer.class))
。
public static void main(String[] args) {
Expectations expectations = new Expectations();
expectations.with(instanceOf(Integer.class));
}
public static <T> Matcher<T> instanceOf(Class<T> type) {
return Matchers.instanceOf(type);
}