带有 Mockito 1.9.5 的 Java 1.8 会出现编译错误 [英] Java 1.8 with Mockito 1.9.5 gives compile errors

查看:66
本文介绍了带有 Mockito 1.9.5 的 Java 1.8 会出现编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到 Java 1.8 后.JDK 我的一些测试类无法编译.实现类示例:

After switching to Java 1.8. JDK some of my test classes fail to compile. Example of implementation class:

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class ImplClass {

    public <T> Future<T> executeTask(final Callable<T> task) {
        return null;
    }
}

这是使用 Mockito 的测试类:

And here is the test class with Mockito:

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.concurrent.Callable;

import org.junit.Before;

public class TestClass {

    private ImplClass implClassMock;

    @Before
    public void setUp() {
        implClassMock = mock(ImplClass.class);
        when(implClassMock.executeTask(any(Callable.class))).thenReturn(null);
    }
}

我收到错误消息:ImplClass 类型中的方法 executeTask(Callable) 不适用于参数 (Callable)

切换回 java 编译器 1.7 一切正常.

Switching back to java compiler 1.7 everything is fine.

知道如何解决这个问题吗?

Any idea how to resolve this issue?

推荐答案

从 java 8 开始,编译器类型推断有了很大的改进.

Since java 8, the compiler type inference have been greatly improved.

现在您可以在没有任何编译警告的情况下从匹配器中删除类参数:

Now you could remove the class parameter from the matcher without any compilation warning:

when(implClassMock.executeTask(any())).thenReturn(null);

<小时>

注意:我有同样的编译器失败,但只有 Eclipse.一个错误可能是?


Note: i have the same compiler failure but only with eclipse. A bug may be ?

这篇关于带有 Mockito 1.9.5 的 Java 1.8 会出现编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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