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

2 Answers 2

2

Oh i fixed it as:

when(mockObject.readFileToMemory(any(FooFile.class), Matchers.<Class<Bar>>any())).thenReturn(new ArrayList<Bar>()) 
Sign up to request clarification or add additional context in comments.

1 Comment

Just a note for other people just finding this: Matchers is deprecated in favor of ArgumentMatchers now.
2

You could also get it working with:

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

1 Comment

No, this throws me error as "Wrong 2nd argument type, required: java.lang.Class<Bar>"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.