简体   繁体   English

Mockito.any() 通过与泛型的接口

[英]Mockito.any() pass Interface with Generics

is it possible to pass the type of an interface with generics?是否可以通过泛型传递接口的类型?

The interface:界面:

public interface AsyncCallback<T>

In my test method:在我的测试方法中:

Mockito.any(AsyncCallback.class)

Putting <ResponseX> behind or for .class didnt work.<ResponseX>放在后面或用于.class不起作用。

有一种类型安全的方法:使用ArgumentMatchers.any()并用类型限定它:

ArgumentMatchers.<AsyncCallback<ResponseX>>any()

Using Java 8, you can simply use any() (assuming static import) without argument or type parameter because of enhanced type inference.使用 Java 8,由于增强的类型推断,您可以简单地使用any() (假设静态导入)而无需参数或类型参数。 The compiler now knows from the target type (the type of the method argument) that you actually mean Matchers.<AsyncCallback<ResponseX>>any() , which is the pre-Java 8 solution.编译器现在从目标类型(方法参数的类型)知道您实际上是指Matchers.<AsyncCallback<ResponseX>>any() ,这是 Java 8 之前的解决方案。

I had to adopt the following mechamism to allow for generics:我不得不采用以下机制来允许泛型:

import static org.mockito.Matchers.any;
List<String> list = any();
when(callMyMethod.getResult(list)).thenReturn(myResultString);

Hope this helps someone.希望这可以帮助某人。

Posting pierrefevrier comment as answer which might be useful if it present in a answer instead of comments.发布 pierrefevrier 评论作为答案,如果它出现在答案中而不是评论中,这可能会很有用。

With new versions of Mockito: (Matchers.<AsyncCallback<ResponseX>>any()使用新版本的 Mockito: (Matchers.<AsyncCallback<ResponseX>>any() Matchers (Matchers.<AsyncCallback<ResponseX>>any()

Further to thSoft's answer putting the qualified call to any() in method meant I could remove the qualification since the return type allowed inference:进一步 thSoft 的回答在方法中对 any() 进行限定调用意味着我可以删除限定,因为返回类型允许推理:

private HashMap<String, String> anyStringStringHashMap() {
    return Matchers.any();
}

I had a similar problem using Spring Example :我在使用 Spring Example遇到了类似的问题:

Mockito.when(repo.findAll(Mockito.<Example<SrvReqToSupplierComment>>any()))
            .thenReturn(Lists.emptyList());

Here, you have to use qualification, b/c findAll method can take multiple types, like Sort and Iterable .在这里,您必须使用限定,b/c findAll 方法可以采用多种类型,例如SortIterable You can also use Mockito.any(Example.class) of course with the type safety warning.当然,您也可以将Mockito.any(Example.class)与类型安全警告一起使用。

You can just cast it, adding suppress warnings if you like:您可以直接投射它,如果您愿意,可以添加抑制警告:

@SuppressWarnings("unchecked")    
AsyncCallback<ResponseX> callback = Mockito.any(AsyncCallback.class)

If Java allowed 'generic' generics they could have a method like this which is what you are looking for如果 Java 允许“通用”泛型,他们可以有一个这样的方法,这就是你要找的

private static <T, E> T<E> mock(Class<T<E>> clazz)

Using a qualified generics type with the no-argument any() method works (ie ArgumentMatchers.<AsyncCallback<ResponseX>>any() ), but can get unwieldy for longer generics expressions.使用带无参数any()方法的限定泛型类型有效(即ArgumentMatchers.<AsyncCallback<ResponseX>>any() ),但对于较长的泛型表达式可能会变得笨拙。 An alternative is to put a no-argument any() call in its own generic method, using the specific generic type as the return type:另一种方法是在它自己的泛型方法中放置一个无参数的any()调用,使用特定的泛型类型作为返回类型:

private static <T> AsyncCallback<T> anyAsyncCallback() {
  return ArgumentMatchers.any()
}

Usage用法

Mockito.verify(mockObject).performCallback(any(), anyAsyncCallback())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM