Java Matchers.any() 用于 Mockito 中的空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32841543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:12:02  来源:igfitidea点击:

Matchers.any() for null value in Mockito

javamockito

提问by Avinash Jadhav

Suppose I am having this object objectDemo which calls to the method objectDemoMethod with 2 parameters String and null. Now I want to verify that this method was called with Mockito:

假设我有这个对象 objectDemo,它使用 2 个参数 String 和 null 调用方法 objectDemoMethod。现在我想验证这个方法是否是用 Mockito 调用的:

objectDemo.objectDemoMethod("SAMPLE_STRING", null);

I have written this:

我写过这个:

Mockito.verify(objectDemo, Mockito.times(1)).objectDemoMethod(Matchers.any(String.class), null);

but it's giving an error:

但它给出了一个错误:

Invalid use of argument matchers for null value.

对空值使用参数匹配器无效。

Is there any another way to pass null value?

还有其他方法可以传递空值吗?

采纳答案by Tunaki

The error message you are getting is expected since you are using argument matcher for only one argument and not the other. From MatchersJavadoc:

您收到的错误消息是预期的,因为您仅将参数匹配器用于一个参数而不是另一个参数。来自MatchersJavadoc:

If you are using argument matchers, all argumentshave to be provided by matchers.

如果您使用参数匹配器,则所有参数都必须由匹配器提供。

Therefore, the fix is to use a matcher for the second parameter of the method as well. In this case, it would be a matcher matching null. Depending on the version of Mockito and Java, you can have:

因此,修复方法是对方法的第二个参数也使用匹配器。在这种情况下,它将是一个匹配器匹配null. 根据 Mockito 和 Java 的版本,您可以拥有:

  • Starting with Mockito 2, you can use ArgumentMatchers.isNull(). This works with Java 8 and above:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), isNull());
    

    Note that if you're running with Java 7 or older, you'll need an explicit cast to make this work, because the type inference in those versions of Java does not take into account the types of the method called:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), (String) isNull());
    
  • If you're using Mockito 1, you can use the Matchers.isNull(clazz)instead:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), isNull(String.class));
    
  • 从 Mockito 2 开始,您可以使用ArgumentMatchers.isNull(). 这适用于 Java 8 及更高版本:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), isNull());
    

    请注意,如果您使用 Java 7 或更早版本运行,则需要显式转换才能使其工作,因为这些版本的 Java 中的类型推断不考虑调用的方法的类型:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), (String) isNull());
    
  • 如果您使用的是 Mockito 1,则可以Matchers.isNull(clazz)改用:

    verify(objectDemo, times(1)).objectDemoMethod(any(String.class), isNull(String.class));
    

For the Java ≤ 7 or Mockito 1 cases, the examples uses a case where the second parameter was of type String: it would need to be replaced with the actual type of the method parameter.

对于 Java ≤ 7 或 Mockito 1 的情况,示例使用的情况是第二个参数的类型为String:它需要替换为方法参数的实际类型。

回答by Amadeu Cavalcante Filho

isNullseems to be deprecated

isNull似乎已被弃用

With Java 8 this method will be removed in Mockito 3.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.

在 Java 8 中,此方法将在 Mockito 3.0 中删除。此方法仅用于通用友好性以避免强制转换,Java 8 中不再需要此方法。

I think you could use nullable:

我认为你可以使用nullable

  • public static <T> T nullable(Class<T> clazz)
  • public static <T> T nullable(Class<T> clazz)

You could use something like:

你可以使用类似的东西:

verify(objectDemo, times(1)).objectDemoMethod(any(String.class), isNull(String.class));

回答by ankit

Just use:

只需使用:

Mockito.verify(objectDemo, Mockito.times(1)).objectDemoMethod(Matchers.any(String.class), (ClassName) isNull());

Mockito.verify(objectDemo, Mockito.times(1)).objectDemoMethod(Matchers.any(String.class), (ClassName) isNull());

Above help me in java 8 version. Hope this will help you.

以上在java 8版本中帮助我。希望这会帮助你。