Mockito.any() 通过泛型接口

发布于 2024-12-12 08:41:55 字数 263 浏览 0 评论 0 原文

是否可以通过泛型传递接口的类型?

界面:

public interface AsyncCallback<T>

在我的测试方法中:

Mockito.any(AsyncCallback.class)

放在 or for .class 后面不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

多情出卖 2024-12-19 08:41:56

我必须采用以下机制来允许泛型:

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

希望这对某人有帮助。

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.

深海不蓝 2024-12-19 08:41:56

将 pierrefevrier 评论发布为答案,如果它出现在答案而不是评论中,这可能会很有用。

使用新版本的 Mockito:(Matchers.>any()

Posting pierrefevrier comment as answer which might be useful if it present in a answer instead of comments.

With new versions of Mockito: (Matchers.<AsyncCallback<ResponseX>>any()

那伤。 2024-12-19 08:41:56

我在使用 Spring Example 时遇到了类似的问题:

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

在这里,您必须使用限定条件,b/c findAll 方法可以采用多种类型,例如 SortIterable.当然,您也可以使用带有类型安全警告的 Mockito.any(Example.class)

I had a similar problem using Spring Example:

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

Here, you have to use qualification, b/c findAll method can take multiple types, like Sort and Iterable. You can also use Mockito.any(Example.class) of course with the type safety warning.

假情假意假温柔 2024-12-19 08:41:56

除了 thSoft 的回答之外,将限定调用放入方法中意味着我可以删除限定,因为返回类型允许推断:

private HashMap<String, String> anyStringStringHashMap() {
    return Matchers.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:

private HashMap<String, String> anyStringStringHashMap() {
    return Matchers.any();
}
北斗星光 2024-12-19 08:41:56

将合格的泛型类型与无参 any() 方法结合使用(即 ArgumentMatchers.>any()),但可能会变得笨拙对于更长的泛型表达式。另一种方法是将无参 any() 调用放入其自己的泛型方法中,并使用特定的泛型类型作为返回类型

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

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

Using a qualified generics type with the no-argument any() method works (i.e. ArgumentMatchers.<AsyncCallback<ResponseX>>any()), but can get unwieldy for longer generics expressions. An alternative is to put a no-argument any() call in its own generic method, using the specific generic type as the return type:

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

Usage

Mockito.verify(mockObject).performCallback(any(), anyAsyncCallback())
梨涡 2024-12-19 08:41:56

你可以直接转换它,如果你愿意的话添加抑制警告:

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

如果Java允许“通用”泛型,他们可能有一个像这样的方法,这就是你正在寻找的

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

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

private static <T, E> T<E> mock(Class<T<E>> clazz)
宛菡 2024-12-19 08:41:55

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

ArgumentMatchers.<AsyncCallback<ResponseX>>any()

There is a type-safe way: use ArgumentMatchers.any() and qualify it with the type:

ArgumentMatchers.<AsyncCallback<ResponseX>>any()
画骨成沙 2024-12-19 08:41:55

使用 Java 8,由于增强的类型推断,您可以简单地使用 any() (假设静态导入)而无需参数或类型参数。编译器现在从目标类型(方法参数的类型)知道您实际上指的是 Matchers.>any(),这是 Java 8 之前的解决方案。

Using Java 8, you can simply use any() (assuming static import) without argument or type parameter because of enhanced type inference. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文