简体   繁体   English

如何对对象使用 Mockito.verify?

[英]How to use Mockito.verify for objects?

I am trying to use Mockito.verify(mock).method(object).我正在尝试使用 Mockito.verify(mock).method(object)。 What I am trying to accomplish is to check if the method was called with object parameter when testing.我想要完成的是在测试时检查是否使用 object 参数调用了该方法。

verify(mock).method(object);

where object is something like:其中 object 类似于:

public class Object {
    private static final string PROPERTY = "property";
}

It seems like value of properties are same, but it still thinks it is different because they are not actual same object.似乎属性的值是相同的,但它仍然认为它是不同的,因为它们实际上不是相同的 object。 What is the best way to handle this?处理这个问题的最佳方法是什么? My initial approach is to use @Captor and check each value one at a time.我最初的方法是使用@Captor 并一次检查每个值。 Is there better way than this?还有比这更好的方法吗?

There are auxiliary methods in Mockito to handle this. Mockito中有辅助方法来处理这个。

For example, define “equals” in the Object and then you can use:例如,在Object中定义“等于”,然后您可以使用:

verify(mock).method(Mockito.eq(object));

Besides eq there are many other methods that might be handy ( same , any* ) etc.除了eq之外,还有许多其他可能很方便的方法( sameany* )等。

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

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