2

My actual method signature is:

public List<T> readFileToMemory(FooFile fooFile, **Class<T> entityClass**) { }

and I am trying to mock this as:

when(mockObject.readFileToMemory(any(FooFile.class), 
         Matchers.any(Class<Bar>)).thenReturn(new ArrayList<Bar>())

but second argument doesn't compile. How to fix it?

I referred to the following answers but still no luck.

Mockito: List Matchers with generics

Mockito: Verifying with generic parameters

Maciej Kowalski
  • 23,799
  • 10
  • 49
  • 59
Alagesan Palani
  • 1,916
  • 3
  • 28
  • 49

2 Answers2

2

Oh i fixed it as:

when(mockObject.readFileToMemory(any(FooFile.class), 
                                 Matchers.<Class<Bar>>any())).thenReturn(new ArrayList<Bar>())
Konstantin Yovkov
  • 60,548
  • 8
  • 97
  • 143
Alagesan Palani
  • 1,916
  • 3
  • 28
  • 49
  • Just a note for other people just finding this: `Matchers` is deprecated in favor of `ArgumentMatchers` now. – ragurney Apr 27 '22 at 15:07
2

You could also get it working with:

when(mockObject.readFileToMemory(any(FooFile.class), eq(Bar.class)))
                                .thenReturn(new ArrayList<Bar>());
Konstantin Yovkov
  • 60,548
  • 8
  • 97
  • 143