使用 Mockito 的通用“any()";方法

Using Mockito#39;s generic quot;any()quot; method(使用 Mockito 的通用“any();方法)
本文介绍了使用 Mockito 的通用“any()";方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个接口,它带有一个需要 Foo 数组的方法:

I have an interface with a method that expects an array of Foo:

public interface IBar {
  void doStuff(Foo[] arr);
}

我正在使用 Mockito 模拟这个接口,我想断言 doStuff() 被调用,但我不想验证传递了什么参数 - 不在乎".

I am mocking this interface using Mockito, and I'd like to assert that doStuff() is called, but I don't want to validate what argument are passed - "don't care".

如何使用通用方法 any() 而不是 anyObject() 编写以下代码?

How do I write the following code using any(), the generic method, instead of anyObject()?

IBar bar = mock(IBar.class);
...
verify(bar).doStuff((Foo[]) anyObject());

推荐答案

从 Java 8 开始,您可以使用无参数的 any 方法,编译器会推断类型参数:

Since Java 8 you can use the argument-less any method and the type argument will get inferred by the compiler:

verify(bar).doStuff(any());


说明

Java 8 中的新功能是 表达式的目标类型 将用于推断其子表达式的类型参数.在 Java 8 之前,只有用于类型参数推断的方法的参数(大部分时间).


Explanation

The new thing in Java 8 is that the target type of an expression will be used to infer type parameters of its sub-expressions. Before Java 8 only arguments to methods where used for type parameter inference (most of the time).

在这种情况下,doStuff的参数类型将是any()的目标类型,any() 将被选择以匹配该参数类型.

In this case the parameter type of doStuff will be the target type for any(), and the return value type of any() will get chosen to match that argument type.

添加此机制主要是为了能够编译 lambda 表达式,但它总体上改进了类型推断.

This mechanism was added mainly to be able to compile lambda expressions, but it improves type inferences generally.

不幸的是,这不适用于原始类型:

This doesn't work with primitive types, unfortunately:

public interface IBar {
    void doPrimitiveStuff(int i);
}

verify(bar).doPrimitiveStuff(any()); // Compiles but throws NullPointerException
verify(bar).doPrimitiveStuff(anyInt()); // This is what you have to do instead

问题是编译器会将Integer推断为any()的返回值类型.Mockito 不会意识到这一点(由于类型擦除)并返回引用类型的默认值,即 null.在将返回值传递给 doStuff 之前,运行时将尝试通过调用 intValue 方法将返回值拆箱,并引发异常.

The problem is that the compiler will infer Integer as the return value type of any(). Mockito will not be aware of this (due to type erasure) and return the default value for reference types, which is null. The runtime will try to unbox the return value by calling the intValue method on it before passing it to doStuff, and the exception gets thrown.

这篇关于使用 Mockito 的通用“any()";方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Show progress during FTP file upload in a java applet(在 Java 小程序中显示 FTP 文件上传期间的进度)
How to copy a file on the FTP server to a directory on the same server in Java?(java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?)
FTP zip upload is corrupted sometimes(FTP zip 上传有时会损坏)
Enable logging in Apache Commons Net for FTP protocol(在 Apache Commons Net 中为 FTP 协议启用日志记录)
Checking file existence on FTP server(检查 FTP 服务器上的文件是否存在)
FtpClient storeFile always return False(FtpClient storeFile 总是返回 False)