嘲笑一种使用mockito返回带有通配符的泛型的方法 [英] mocking a method that return generics with wildcard using mockito

查看:1554
本文介绍了嘲笑一种使用mockito返回带有通配符的泛型的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mockito 1.9.5。
我有以下代码:

  public class ClassA {

public List< ;?扩展MyInterface> getMyInterfaces(){
返回null;
}

public static void testMock(){
List< MyInterface> interfaces = new ArrayList<>();
ClassA classAMock = mock(ClassA.class);
when(classAMock.getMyInterfaces())。thenReturn(interfaces);

$ / code>

我得到然后返回(接口)说:

 然后返回方法(List< capture#1 of?extends MyInterface> )
OngoingStubbing< List< capture#1-of?extends MyInterface>>不适用于参数
(List< MyInterface>)

然而,当我使用mockito的 thenAnswer 方法时,我没有得到错误。谁能告诉我发生了什么事?为什么我在使用然后返回方法时遇到错误?
当第三方提供 ClassA 并且无法修改时,是否有其他方法可以解决此问题?

:从Mockito 1.10.x开始,嵌入类中的泛型类型现在被Mockito用于深存根。即。

  public interface A< T扩展了Observer&可比< ;? super T>> {
列表< ;?扩展B> bList();
T观察者();
}

B b = deep_stubbed.bList()。iterator()。next(); //返回B的模拟; mockito记得A返回一个B
的列表Observer o = deep_stubbed.observer(); // mockito可以发现T超类型是Observer
Comparable< ;?超T> c = deep_stubbed.observer(); //或者T实现Comparable

Mockito尽力获取编译器嵌入的类型信息,但当删除适用时,mockito不能做任何事情,但返回一个模拟的 Object






原始:那么泛型与Mockito相比,这就更是一个问题。对于泛型,您应该阅读 Angelika Langer 在他们上面写的内容。对于当前主题,即通配符,请阅读此部分



但是,简而言之,您可以使用Mockito的其他语法来帮助您解决当前的情况:

  doReturn(接口)。当(classAMock).getMyInterfaces(); 

或者使用BDD别名:

  willReturn(接口)。鉴于(classAMock).getMyInterfaces(); 

然而,您可以编写更通用的包装器。这将有助于未来的开发人员使用相同的第三方API。






作为一个附注:你不应该嘲笑你不拥有,它可能导致许多错误和问题。相反,你应该有一些包装。例如DAO和存储库代表了这样的想法,人们会模拟DAO或存储库接口,而不是JDBC / JPA / Hibernate的东西。有很多关于这方面的博客文章:


I'm using mockito 1.9.5. I have the following code:

public class ClassA  {

public List<? extends MyInterface> getMyInterfaces() {
    return null;
}

public static void testMock() {
    List<MyInterface> interfaces = new ArrayList<>();
    ClassA classAMock = mock(ClassA.class);
    when(classAMock.getMyInterfaces()).thenReturn(interfaces);      
}

I get a compilation error for the thenReturn(interfaces) saying:

"The method thenReturn(List<capture#1-of ? extends MyInterface>) in the type 
 OngoingStubbing<List<capture#1-of ? extends MyInterface>> is not applicable for the arguments 
 (List<MyInterface>)"

However, when I use the thenAnswer method of mockito, I don't get the error. Can anyone tell me what's going on? Why do I get the error when I use the thenReturn method? Is there any other way to solve this problem when ClassA is provided by a 3rd party and cannot be modified?

解决方案

EDIT : Starting from Mockito 1.10.x, generics types that are embedded in the class are now used by Mockito for deep stubs. ie.

public interface A<T extends Observer & Comparable<? super T>>  {
  List<? extends B> bList();
  T observer();
}

B b = deep_stubbed.bList().iterator().next(); // returns a mock of B ; mockito remebers that A returns a List of B
Observer o = deep_stubbed.observer(); // mockito can find that T super type is Observer
Comparable<? super T> c = deep_stubbed.observer(); // or that T implements Comparable

Mockito tries its best to get type information that the compiler embeds, but when erasure applies, mockito cannot do anything but return a mock of Object.


Original : Well that's more of an issue with generics than with Mockito. For generics, you should read what Angelika Langer wrote on them. And for the current topic, i.e. wildcards, read this section.

But for short, what you could use is the other syntax of Mockito to help with your current situation :

doReturn(interfaces).when(classAMock).getMyInterfaces();

Or with the BDD aliases :

willReturn(interfaces).given(classAMock).getMyInterfaces();

Nevertheless, you could write wrappers that are more generic friendly. That will help future developers working with same 3rd party API.


As a side note: you shouldn't mocks type you don't own, it can lead to many errors and issues. Instead you should have some wrapper. DAO and repositories for example represent such idea, one will mock the DAO or repository interface, but not the JDBC / JPA / hibernate stuff. There are many blog posts about that:

这篇关于嘲笑一种使用mockito返回带有通配符的泛型的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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