lundi 27 juin 2016

Java Mockito: Test a protected abstract method

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.

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.

My test class is as follows:

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest extends AbstractScraperTest<HtmlPage, List<myItems>>{

    @Mock private SsoSetting ssoSetting;
    @Mock private SsoSetting.UrlSettings urlSetting;
    @Mock 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<Map> postRequestCaptor = ArgumentCaptor.forClass(Map.class);
    final ArgumentCaptor<Scraper> 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<productItems> result = testScraper("htmlResource","myUrlToTest");
    }
}

The abstract class:

public abstract class AbstractScraperTest<Input, Output> {
    public ServiceForScraping serviceScrape;
    public Scraper<Input, Output> scraper;

    protected abstract Scraper<Input, Output> 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);
    }
}

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:

Example

@Test(expected=customException.class)
    public void scrape_TimeOut_Exception() throws Exception {
    List<CartItem> result = testScraper("htmlResource","myUrl");
}

The test should validate to true as the html resource does contain session timeout information.

The problem I believe when I debug on

return serviceScrape.scrapeString(url, html, scraper);

The scraper returns null. I have tried doing things such as

AbstractScraperTest.scraper = scraper;

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.

Links Looked into that I remember:

  1. mocking protected method
  2. How to mock protected subclass method inherited from abstract class?
  3. How can I test a method which invoke protected (unwanted) methods of parent class?
  4. http://ift.tt/28ZtI34
  5. http://ift.tt/293jyBr
  6. http://ift.tt/1qsVnNQ

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.

Any help, ideas, references are greatly appreciated. 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. Thanks in advance, and if you have any questions I will respond and can update the post quickly.

Thanks again,

Aucun commentaire:

Enregistrer un commentaire