如何使用 Mockito 的anyCollectionOf()
此线程上的答案建议使用 anyCollectionOf() 但我无法让它工作 Mockito:使用通用参数进行验证
我有一个通用类,用于保存两个“版本”用于比较目的的相同对象
public class ChangedVO<T> {
private T before;
private T after;
/*** Constructors ***/
public ChangedVO() {}
public ChangedVO(T before, T after) {
this.before = before;
this.after = after;
}
/*** Getters & setters ***/
...
}
现在在我的 UnitTest 中,以下代码“确实”工作......
verify(emailService, never()).sendBookChangesEmail(Matchers.<List<ChangedVO<BookVO>>>any());
但是我有兴趣知道如何使用 anyCollectionOf() 实现相同的事情?
An answewr on this thread suggested using anyCollectionOf() however I cannot get it to work
Mockito: Verifying with generic parameters
I have a Generic Class for holding two "versions" of the same object for comparison purposes
public class ChangedVO<T> {
private T before;
private T after;
/*** Constructors ***/
public ChangedVO() {}
public ChangedVO(T before, T after) {
this.before = before;
this.after = after;
}
/*** Getters & setters ***/
...
}
Now in my UnitTest the following code "does" work...
verify(emailService, never()).sendBookChangesEmail(Matchers.<List<ChangedVO<BookVO>>>any());
...however I'm interested to know how the same thing can be achieved using anyCollectionOf() ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mockito 与泛型的有趣使用。我认为在这种情况下可以使用
anyListOf()
的唯一方法是键入而不是传递类实例,而是将其传递为 null。这不是文档所说的应该使用
anyListOf()
的方式,但在这种特定情况下它似乎可以工作。问题是 Java 中的泛型类型擦除,这使得您无法在运行时区分 的实例
,因此您无法创建任何静态符合
Interesting use of Mockito with generics. The only way I think you can use
anyListOf()
in this situation is by typingInstead of passing a class instance you pass it null. This is not how the documentation says you shoud use
anyListOf()
but in this specific situation it seems to work.The problem is generic type erasure in Java which makes that you can not differentiate between an instance of
during runtime and therefore there is no way you can create anything that statically conforms to