How to test whether a method has been overriden in junit?

99封情书 提交于 2019-12-07 10:10:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!