我試圖寫一個測試下列靜態方法:驗證的Mockito對Class對象NotAMockException
public static Field getField (Class<?> type, String fieldName) {
for (Field field : type.getDeclaredFields()) {
if (field.getName().equals(fieldName)) {
return field;
}
}
if (type.getSuperclass() != null) {
return getField(type.getSuperclass(), fieldName);
}
return null;
}
這是我的測試代碼:
@Test
public void getFieldTest() {
class StupidObject { Object stupidField; }
class ReallyStupidObject extends StupidObject { Object reallyStupidField; }
ReallyStupidObject reallyStupidObject = mock(ReallyStupidObject.class);
// --------------------------------------------------
Field field = FSUtils.getField(reallyStupidObject.getClass(), "stupidField");
// --------------------------------------------------
verify(reallyStupidObject.getClass()).getDeclaredFields();
verify(reallyStupidObject.getClass()).getSuperclass();
verify(reallyStupidObject.getClass().getSuperclass()).getDeclaredFields();
verify(reallyStupidObject.getClass().getSuperclass(), never()).getSuperclass();
assertEquals(field.getName(), "stupidField");
}
導致這個錯誤:
org.mockito.exceptions.misusing.NotAMockException: Argument passed to verify() is of type Class and is not a mock!
任何人都可以闡明我如何驗證在Class對象上調用的方法嗎?
非常感謝!
本。
這看起來不錯,謝謝! – Ben