Verifying super.method() is called using Mockito

DeaIss picture DeaIss · Dec 11, 2013 · Viewed 11.2k times · Source

First of all I would like to say I am working with legacy code and I cannot change it no matter how much I want to.

With that out the way, what I am trying to do is verify that a super.method() is called. This is specifically what I am trying to test with Mockito/Junit:

class foo extends JApplet(){

       public void destroy(){
          super.destroy();
 }
}

Normally something like this would suffice in a test case if it was not a super method being called:

verify(foo).destroy();

I've seen this question asked a couple of times and usually the response is "Inheritance is bad, change your code" which I cannot do at all unfortunately. Is anyone aware of any frameworks or small little tricks I could do to test this?

Thanks in advance - I know this is a tricky problem!

Answer

Vishal picture Vishal · Jul 15, 2014

You can refactor the code to call the super method in some other method like:

      class foo extends JApplet(){

       public void destroy(){
          callSuperDestroy();
       }
       public void callSuperDestroy(){
           super.destroy();
       }}

And then verify:

verify(foo).callSuperDestroy();