带有(instanceOf(Integer.class))的JMock不能在Java 8中编译 [英] JMock with(instanceOf(Integer.class)) does not compile in Java 8

查看:108
本文介绍了带有(instanceOf(Integer.class))的JMock不能在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 what with() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆