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
  • 25,605
  • 12
  • 54
  • 63
Alagesan Palani
  • 1,984
  • 4
  • 28
  • 53

2 Answers2

2

Oh i fixed it as:

when(mockObject.readFileToMemory(any(FooFile.class), 
                                 Matchers.<Class<Bar>>any())).thenReturn(new ArrayList<Bar>())
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Alagesan Palani
  • 1,984
  • 4
  • 28
  • 53
  • 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
  • 62,134
  • 8
  • 100
  • 147