相关疑难解决方法(0)

Mockito匹配器如何工作?

争论的Mockito匹配器(如any,argThat,eq,same,和ArgumentCaptor.capture())表现非常不同,从Hamcrest匹配器.

  • Mockito匹配器经常导致InvalidUseOfMatchersException,即使在使用任何匹配器后执行很长时间的代码中也是如此.

  • Mockito匹配器受到奇怪的规则的影响,例如,如果给定方法中的一个参数使用匹配器,则只需要对所有参数使用Mockito匹配器.

  • 当覆盖Answers或使用(Integer) any()等时,Mockito匹配器可能会导致NullPointerException .

  • 使用Mockito匹配器以某种方式重构代码可能会产生异常和意外行为,并且可能完全失败.

为什么Mockito匹配器是这样设计的,它们是如何实现的?

java mocking mockito

112
推荐指数
2
解决办法
11万
查看次数

为什么这个通用代码在java 8中编译?

我偶然发现了一段代码,让我想知道为什么它成功编译:

public class Main {
    public static void main(String[] args) {
        String s =  newList(); // why does this line compile?
        System.out.println(s);
    }

    private static <T extends List<Integer>> T newList() {
        return (T) new ArrayList<Integer>();
    }
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,如果我修改方法的签名newList<T extends ArrayList<Integer>>它不工作了.

注释和响应后更新: 如果我将泛型类型从方法移动到类,则代码不再编译:

public class SomeClass<T extends List<Integer>> {
    public  void main(String[] args) {
        String s = newList(); // this doesn't compile anymore
        System.out.println(s);
    }

    private T newList() {
        return (T) new ArrayList<Integer>();
    }
}
Run Code Online (Sandbox Code Playgroud)

java generics compiler-errors java-8

42
推荐指数
3
解决办法
4254
查看次数

标签 统计

java ×2

compiler-errors ×1

generics ×1

java-8 ×1

mocking ×1

mockito ×1