2

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() ?

Community
  • 1
  • 1
Brad
  • 15,186
  • 11
  • 60
  • 74

1 Answers1

2

Interesting use of Mockito with generics. The only way I think you can use anyListOf() in this situation is by typing

verify(emailService, never()).sendBookChangesEmail(anyListOf((Class<ChangedV0<BookV0>>) null));

Instead 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

Class<ChangedV0<BookVO>> or
Class<ChangedV0>

during runtime and therefore there is no way you can create anything that statically conforms to

Class<ChangedV0<BookVO>>
Arno Fiva
  • 1,459
  • 1
  • 13
  • 17
  • Thanks for your feedback fiv. I prefer your solution to the Matchers one that I proposed. It reads better. – Brad Jun 17 '11 at 09:24