title: Mockito - mock subclass method call
body: <p>I am working with legacy code and trying to write a unit test for an implementation I did. I have the following structure:</p>
  
  <pre><code>   AbstractClass:
      ----------------
  
      public AbstractClass {
  
      abstract method2();
  
       void method1(){   
        ...does something...
        ..calls --&gt; method2()
       }
      }
  
      Subclass
      ------------
      public Subclass extends AbstractClass(){
  
       void method2(){
        ..do something....
        ..mockThisMethod()...
  
         return true;   
       }
   }
  </code></pre>
  
  <p>I have an abstract class which has some logic implemented in a specific method.
  This method calls than another method implemented in the subclass. The method in the subclass calls other methods which I want to mock.</p>
  
  <p>Is this possible without changing the implementation code (hard to change)?
  Mos of the results are suggesting using mockito spying but it doesn't work.</p>
  
  <p>I followed the TestDesign describer here:</p>
  
  <p><a href="https://www.tildedave.com/2011/03/06/pattern-stubbing-legacy-superclasses-with-mockito-spies.html" rel="nofollow noreferrer">https://www.tildedave.com/2011/03/06/pattern-stubbing-legacy-superclasses-with-mockito-spies.html</a></p>
  
  <h2>What I did is:</h2>
  
  <pre><code> Subclass subclass= spy(Subclass.class);
   when(subclass.mockThisMethod()).thenReturn(something);
   subclass.method1() (I am not sure if this line is correct?)
  </code></pre>
  
  <p>So what I want to avoid is, calling the method (<code>mockThisMethod</code>) in the <code>Subclass</code>.
  Since this method does some db-stuff an so on. I know it would be easier to test, if I use object composition instead of inheritance but at this point, it is hard to change the whole implementation. The code above is not working..I get a NPE. </p>