title: Java Mockito: Test a protected abstract method
body: <p>I have read many posts the last few days and there are some that are trying to do what I am trying to accomplish however they are on a class that is extending a class with a protected method. However mine is different. </p>
  
  <p>I currently have a test class that extends and Mocks the abstract class with the protected method. No matter how I try to implement the value for the one keeps returning null and I cannot figure out why this is occurring. In turn this is returning a nullPointerEception rather then my custom exception which is should hit.</p>
  
  <p><strong>My test class is as follows:</strong></p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class ServiceTest extends AbstractScraperTest&lt;HtmlPage, List&lt;myItems&gt;&gt;{
  
      @Mock private SsoSetting ssoSetting;
      @Mock private SsoSetting.UrlSettings urlSetting;
      // @Mock(answer = Answers.CALLS_REAL_METHODS) private ServiceForScraping serviceScrape;
      @Mock Scraper scraper;
      @Mock AbstractScraperTest abstractScraperTest;
      @Mock private ProductScrape productScrape;
      @Mock private ProductDetailScrape productDetailScrape;
  
      @InjectMocks private Service service;
  
      @Before
      public void setUp() throws Exception {
          when(ssoSetting.getUrls()).thenReturn(urlSetting);
      }
  
      final String URL = "myUrl";
      final ArgumentCaptor&lt;Map&gt; postRequestCaptor = ArgumentCaptor.forClass(Map.class);
      final ArgumentCaptor&lt;Scraper&gt; scrapeCaptor = ArgumentCaptor.forClass(Scraper.class);
  
      @Test(expected = customException.class)
      public void scrape_TimeOut_Exception() throws Exception {
  
      //
      //        AbstractScraperTest ab = Mockito.mock(AbstractScraperTest.class, Mockito.CALLS_REAL_METHODS);
      //
      //    assertEquals(ab.testScraper("htmlResource",
      //                "myUrlToTest"), customException.class);
      //
      //    List&lt;productItems&gt; result = testScraper("htmlResource","myUrlToTest");
      }
  }
  </code></pre>
  
  <p><strong>The abstract class:</strong></p>
  
  <pre><code>public abstract class AbstractScraperTest&lt;Input, Output&gt; {
      public ServiceForScraping serviceScrape;
      public Scraper&lt;Input, Output&gt; scraper;
  
      protected abstract Scraper&lt;Input, Output&gt; getScraper();
  
      @Before
      public void setUp() throws Exception {
          scraper = getScraper();
          serviceScrape = new ServiceForScraping ();
          ServiceForScraping .init();
      }
  
      protected Output testScraper(String filePath, String url) throws Exception {
          InputStream input = getClass().getResourceAsStream(filePath);
          String html = IOUtils.toString(input);
  
          return serviceScrape.scrapeString(url, html, scraper);
      }
  }
  </code></pre>
  
  <p><strong>The Class testing for the Exception:</strong></p>
  
  <pre><code>public abstract class myScraper&lt;Output&gt; implements     HTMLUnitScraper&lt;Output&gt; {
  
      @Override
      public Output scrape(HtmlPage page, String scraperUrl) throws Exception  {
          checkSessionTimeout(page, scraperUrl);
          return scrapeHtmlPage(page, scraperUrl);
      }
  
      private void checkSessionTimeout(HtmlPage page, String scraperUrl) throws Exception {
  
          if (page.getFirstByXPath("//classImLookingFor']") != null
              &amp;&amp; page.getFirstByXPath("//ClassImLookingFor") != null) {
              throw new customExceptionThrown("Session Timeout" + scraperUrl);
          }
      }
  
      public abstract Output scrapeHtmlPage(HtmlPage page, String scraperUrl)  throws Exception;
  }
  </code></pre>
  
  <p>The abstract classes method 'testScrape' tests if the html resource contains timeout session information. I have used this method in other tests that do not use Mockito and they have all worked: </p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>@Test(expected=customException.class)
      public void scrape_TimeOut_Exception() throws Exception {
      List&lt;CartItem&gt; result = testScraper("htmlResource","myUrl");
  }
  </code></pre>
  
  <p>The test should validate to true as the html resource does contain session timeout information. </p>
  
  <p>The problem I believe when I debug on </p>
  
  <pre><code>return serviceScrape.scrapeString(url, html, scraper);
  </code></pre>
  
  <p>The scraper returns null. I have tried doing things such as </p>
  
  <pre><code>AbstractScraperTest.scraper = scraper;
  </code></pre>
  
  <p>as well moving the calls in the @before in abstractScraperTest into the @before in the ServiceTest class however that does not seem to work as well. I am not sure why I am returning null and cant put a value in it, I believe this is why it is failing.</p>
  
  <p>Links Looked into that I remember:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/8312212/mocking-protected-method">mocking protected method</a></li>
  <li><a href="https://stackoverflow.com/questions/32073116/how-to-mock-protected-subclass-method-inherited-from-abstract-class">How to mock protected subclass method inherited from abstract class?</a></li>
  <li><a href="https://stackoverflow.com/questions/25363884/how-can-i-test-a-method-which-invoke-protected-unwanted-methods-of-parent-clas">How can I test a method which invoke protected (unwanted) methods of parent class?</a></li>
  <li><a href="http://huahsin68.blogspot.ca/2014/01/invoke-protected-method-with.html" rel="nofollow noreferrer">http://huahsin68.blogspot.ca/2014/01/invoke-protected-method-with.html</a></li>
  <li><a href="https://groups.google.com/forum/#!topic/powermock/DtMMHa9k-4Q" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/powermock/DtMMHa9k-4Q</a></li>
  <li><a href="http://www.vogella.com/tutorials/Mockito/article.html" rel="nofollow noreferrer">http://www.vogella.com/tutorials/Mockito/article.html</a></li>
  </ol>
  
  <p>I did read into one post that was saying to use PowerMockito however I could not get the methods and implementations for that to work. </p>
  
  <p>I am fairly sure this has to be possible for what I am trying to achieve as other ones are fairly similar however I cannot figure the correct implementation for it.</p>