Mockito mock with when返回null

时间:2017-06-03 13:45:18

标签: java unit-testing mocking mockito

我模拟articleElementSelector.getTag()返回一个字符串并使用InjectMocks注释将模拟注入构造函数。在调试器中,我看到articleElementSelector被模拟(因为CGLib的东西),但是当调用getTag()时,它返回null。

HomePageScraperTest:

public class HomePageScraperTest extends UnitTest {

    private static final String ARTICLE_TAG = "article";
    private static final String URL_HOME_ARTICLE_1 = "http://www.home1.com";
    private static final String URL_HOME_ARTICLE_2 = "http://www.home2.com";

    @InjectMocks
    private HomePageScraper homePageScraper;

    @Mock
    private JsoupParser jsoupParser;
    @Mock
    private ArticleScraper articleScraper;
    @Mock
    private ArticleElementSelector articleElementSelector;

    @Mock
    private Document homeDocument;
    @Mock
    private Element element1;
    @Mock
    private Element element2;
    @Mock
    private Elements elements1;
    @Mock
    private Elements elements2;

    private URL homeUrl;
    private Elements homeArticleElements = new Elements();
    private Article homeArticle1;
    private Article homeArticle2;

    @Before
    public void setUp() throws Exception {
        homeUrl = new URL(URL_HOME_ARTICLE_1);
        homeArticleElements.addAll(asList(element1, element2));
        homeArticle1 = anArticle().withTitle("article1").build();
        homeArticle2 = anArticle().withTitle("article2").build();
    }

    @Test
    public void scrape() {
        HomePage homePage = new HomePage(homeUrl);
        when(articleElementSelector.getTag()).thenReturn(ARTICLE_TAG);
        when(jsoupParser.parse(homeUrl)).thenReturn(homeDocument);
        when(homeDocument.select(ARTICLE_TAG)).thenReturn(homeArticleElements);
        when(element1.select("a")).thenReturn(elements1);
        when(elements1.attr("href")).thenReturn(URL_HOME_ARTICLE_1);
        when(element2.select("a")).thenReturn(elements2);
        when(elements2.attr("href")).thenReturn(URL_HOME_ARTICLE_2);
        when(articleScraper.scrape(URL_HOME_ARTICLE_1)).thenReturn(homeArticle1);
        when(articleScraper.scrape(URL_HOME_ARTICLE_2)).thenReturn(homeArticle2);

        List<Article> articles = homePageScraper.scrape(homePage);

        assertThat(articles).containsOnly(homeArticle1, homeArticle2);
    }
}

HomePageScraper(仅限相关代码)

@Component
public class HomePageScraper extends AbstractPageScraper {

    private static final int HEADLINER_COUNT = 5;

    public HomePageScraper(JsoupParser parser, ArticleElementSelector articleElementSelector, ArticleScraper articleScraper) {
        super(parser, articleElementSelector, articleScraper);
    }
}

AbstractPageScraper

public abstract class AbstractPageScraper {

    private final String ARTICLE_TAG;

    private JsoupParser parser;

    ArticleScraper articleScraper;

    public AbstractPageScraper(JsoupParser parser, ArticleElementSelector articleElementSelector, ArticleScraper articleScraper) {
        this.parser = parser;
        ARTICLE_TAG = articleElementSelector.getTag(); // here the mock returns null
        this.articleScraper = articleScraper;
    }
}

测试工作之前,但在我重构之后,在抽象类中提取重复的代码,我碰到了这个。

0 个答案:

没有答案