How to check if a method has been overridden in junit?

I have this question, is there a test annotation or statement that can determine if the method has been overridden in junit?

I am currently running a test case that should determine if the Foo class has redefined the toString() method of its superclass. Thanks.

+4
source share
1 answer

you could probably do it like this:

  class.getMethod("toString").getDeclaringClass(); 

Here is an example:

  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 overriding toString, then comment it out and you will see the output and "Test", then "Object"

+5
source

All Articles