带有(instanceOf(Integer.class))的JMock不能在Java 8中编译 [英] JMock with(instanceOf(Integer.class)) does not compile in Java 8
问题描述
使用(Matcher< Object>)的方法;)对于新的Expectations(){}
是不明确的,这是由这个方法调用引起的:
import org.jmock.Expectations;
与()所期望的
public class Ambiguous {
public static void main(String [] args){
Expectations expected = new Expectations();
expected.with(org.hamcrest.Matchers.instanceOf(Integer.class));
$ b好像是从<$ c $返回c> instanceOf()
不一致,反之亦然。有没有办法解决这个问题?
解决方案有一些简单的方法可以帮助编译器。 b
将匹配器赋值给一个局部变量:
public static void main(String [] args) {
期望预期=新期望();
匹配器<整数> instanceOf = Matchers.instanceOf(Integer.class);
expectations.with(instanceOf);
}
您可以使用类型见证来指定类型参数,如下所示:
public static void main(String [] args){
Expectations expected = new Expectations();
expected.with(Matchers。< Integer> instanceOf(Integer.class));
将instanceOf封装在您自己的类型安全方法中:
public static void main(String [] args){
Expectations expected = new Expectations();
expected.with(instanceOf(Integer.class));
}
public static< T>匹配< T> instanceOf(Class< T> type){
返回Matchers.instanceOf(type);
}
我更喜欢最后的解决方案,因为它是可重用的,测试仍然易于阅读。
After upgrading to Java 8. I now have compile errors of the following kind:
The method with(Matcher<Object>) is ambiguous for the type new Expectations(){}
It is caused by this method call:
import org.jmock.Expectations; public class Ambiguous { public static void main(String[] args) { Expectations expectations = new Expectations(); expectations.with(org.hamcrest.Matchers.instanceOf(Integer.class)); } }
It seems like with is returned from
instanceOf()
is ambiguous from whatwith()
expects, or vice versa. Is there a way to fix this?解决方案There is some easy ways to help the compiler.
assign the matcher to a local variable:
public static void main(String[] args) { Expectations expectations = new Expectations(); Matcher<Integer> instanceOf = Matchers.instanceOf(Integer.class); expectations.with(instanceOf); }
you can specify the type parameter with a type witness as follows:
public static void main(String[] args) { Expectations expectations = new Expectations(); expectations.with(Matchers.<Integer>instanceOf(Integer.class)); }
wrap the instanceOf in your own type safe method:
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); }
I prefer the last solution since it is reusable and the test remains easy to read.
这篇关于带有(instanceOf(Integer.class))的JMock不能在Java 8中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!