问题
I have this question, is there a test annotation or assertion which can tell if a method has been overriden in junit?
I'm currently implementing a test case which should tell whether the class Foo
's method toString()
has overriden its super class.
Thanks.
回答1:
you could do it like this probably:
class.getMethod("toString").getDeclaringClass();
Here is a sample:
class Test {
public String toString(){
return "From test";
}
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
String resut = Test.class.getMethod("toString").getDeclaringClass().getName();
System.out.println(resut);
}
Now run this with toString firs overridden then comment it out and you will see the output and "Test" then "Object"
来源:https://stackoverflow.com/questions/15996311/how-to-test-whether-a-method-has-been-overriden-in-junit