Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying super.method() is called using Mockito

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!

like image 235
DeaIss Avatar asked Dec 11 '13 15:12

DeaIss


People also ask

Does Mockito verify call the method?

By default, Mockito verifies that the method was called once, but you can verify any number of invocations: // verify the exact number of invocations verify(passwordEncoder, times(42)).

How verify method works in Mockito?

Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method. We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified.


1 Answers

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();
like image 107
Vishal Avatar answered Sep 27 '22 22:09

Vishal