2

I have this code:

when(mockedObject.play(any(Class.class))).thenReturn(object);

The header of the method play is like this:

public Object play(Class<T> classz)

When I run my test I get this error:

The method play(Class<T>) is not applicable for the arguments (Class)

How should I fix this?

nickb
  • 59,313
  • 13
  • 108
  • 143
dardy
  • 433
  • 1
  • 6
  • 18

2 Answers2

3

This question is similar. You want:

when(mockedObject.play(Matchers.<Class<T>>any())).thenReturn(object);

Or in Java 8 you can just call:

when(mockedObject.play(Matchers.any())).thenReturn(object);

Assuming that T is a generic of the object you've mocked, you'll want to replace it with whatever you've instantiated mockedObject with. So, if you've got something like this:

@Mock
Foo<Bar> mockedObject;

You'll want to use:

when(mockedObject.play(Matchers.<Class<Bar>>any())).thenReturn(object);
Community
  • 1
  • 1
AndyN
  • 2,075
  • 16
  • 25
  • I found in the same discussion that using Java 8, I can simply use `any()`. it works :) Thanks! – dardy Jun 15 '16 at 13:31
  • yes, correct. In Java 8, the Generalized Target-type Inference improvement can work out what generic is appropriate. – AndyN Jun 15 '16 at 13:32
0

Try casting, by doing this:

when(mockedObject.play((Class<?>)any(Class.class))).thenReturn(object);
Aheza Desire
  • 509
  • 5
  • 4