This doesn't seem like a problem that should be solved with mocking. I would opt for a concrete test like this:
@Test
public void getFieldTest() {
class StupidObject { Object stupidField; }
class ReallyStupidObject extends StupidObject { Object reallyStupidField; }
// test the right field is found in the right class
testFieldFind("stupidField", ReallyStupidObject.class,
StupidObject.class);
testFieldFind("reallyStupidField", ReallyStupidObject.class,
ReallyStupidObject.class);
// check that non-existent fields return null
assertNull(FSUtils.getField(ReallyStupidObject.class, "fooballs"));
}
private static void testFieldFind(String fieldName, Class<?> classToSearch,
Class<?> expectedDeclaringClass) {
Field field = FSUtils.getField(classToSearch, fieldName);
assertEquals(fieldName, field.getName());
assertEquals(expectedDeclaringClass, field.getDeclaringClass());
}
As to why your original code failed - you have to call verify
on your mocked object, i.e. verify(reallyStupidObject).doSomething()
.