In Java, I want to write a test for a method (simplified snippet):
public class MyClass {
private static final Set<Class> SOME_SET = new HashSet<Class>(Arrays.asList(Foo.class, Bar.class));
public boolean isValid(Class clazz){
return SOME_SET.contains(clazz);
}
}
Problem with the next test.
import static org.mockito.Mockito.when;
import org.mockito.Mockito;
public class MyClassTest {
@Test
public void isValid_Foo_returnsTrue(){
Foo foo = Mockito.mock(Foo.class);
MyClass target = new MyClass();
assertTrue(target.isValid(foo));
}
}
- this is that on the mocked class, Foo foo.getClass()returns the name of the class with an additional suffix. Something like that:
Foo$$EnhancerByMockitoWithCGLIB$$45508b12
For this reason, the test failed because SOME_SET.contains (clazz) returns false.
I was not able to make fun of the getClass () method in Foo:
Mockito.when(foo.getClass()).thenReturn(Foo.class);
Because the compiler complained: thenReturn (Class <capture # 1-of? Extends Foo>) method in type OngoingStubbing <Class <capture # 1-of? extends Foo -> not applicable for arguments (Class <Foo>)
The question is how to get the wearClass's getClass () method to return the same value as the getClass ()'s method for a real (not mocking) object ?