将元素添加到模拟列表

mara122:

我正在尝试对负责添加到地图分类书籍的方法进行单元测试。

@Service
public class BookService {

    private final List<BookServiceSource> sources;

    @Autowired
    public BookService(List<BookServiceSource> sources) {
        this.sources = sources;
    }

    public Map<Bookstore, List<Book>> getBooksByCategory(CategoryType category) {
        return sources.stream()
                .collect(Collectors.toMap(BookServiceSource::getName,
                        source -> source.getBooksByCategory(category)));
    }
    }

BookSerivceSource是一个接口。该接口由两个类实现。我只提供其中一个,因为第二个确实很相似。

EmpikSource(实现之一)

package bookstore.scraper.book.booksource.empik;

import bookstore.scraper.book.Book;
import bookstore.scraper.book.booksource.BookServiceSource;
import bookstore.scraper.enums.Bookstore;
import bookstore.scraper.enums.CategoryType;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import bookstore.scraper.utilities.JSoupConnector;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.IntStream;

@Service
public class EmpikSource implements BookServiceSource {

    private static final int FIRST_PART_PRICE = 0;
    private static final int SECOND_PART_PRICE = 1;

    private static final int BESTSELLERS_NUMBER_TO_FETCH = 5;
    private static final int CATEGORIZED_BOOKS_NUMBER_TO_FETCH = 15;
    private static final String DIV_PRODUCT_WRAPPER = "div.productWrapper";
    private static final String DATA_PRODUCT_ID = "data-product-id";

    private final EmpikUrlProperties empikUrlProperties;
    private final JSoupConnector jSoupConnector;
    private Map<CategoryType, String> categoryToEmpikURL;

    @Autowired
    public EmpikSource(EmpikUrlProperties empikUrlProperties, JSoupConnector jSoupConnector) {
        this.empikUrlProperties = empikUrlProperties;
        this.jSoupConnector = jSoupConnector;
        categoryToEmpikURL = createCategoryToEmpikURLMap();
    }

    @Override
    public Bookstore getName() {
        return Bookstore.EMPIK;
    }

    @Override
    public List<Book> getBooksByCategory(CategoryType categoryType) {
        Document document = jSoupConnector.connect(categoryToEmpikURL.get(categoryType));

        List<Book> books = new ArrayList<>();
        List<Element> siteElements = document.select("div.productBox__info");

        IntStream.range(0, CATEGORIZED_BOOKS_NUMBER_TO_FETCH)
                .forEach(iteratedElement -> {

                    String author = executeFetchingAuthorProcess(siteElements, iteratedElement);
                    String price = convertEmpikPriceWithPossibleDiscountToActualPrice(siteElements.get(iteratedElement).select("div.productBox__price").first().text());
                    String title = siteElements.get(iteratedElement).select("span").first().ownText();
                    String productID = siteElements.get(iteratedElement).select("a").first().attr(DATA_PRODUCT_ID);
                    String bookUrl = createBookURL(title, productID);

                    books.add(Book.builder()
                            .author(author)
                            .price(price)
                            .title(title)
                            .productID(productID)
                            .bookURL(bookUrl)
                            .build());
                });

        return books;
    }


    private Map<CategoryType, String> createCategoryToEmpikURLMap() {
        Map<CategoryType, String> map = new EnumMap<>(CategoryType.class);

        map.put(CategoryType.CRIME, empikUrlProperties.getCrime());
        map.put(CategoryType.BESTSELLER, empikUrlProperties.getBestSellers());
        map.put(CategoryType.BIOGRAPHY, empikUrlProperties.getBiographies());
        map.put(CategoryType.FANTASY, empikUrlProperties.getFantasy());
        map.put(CategoryType.GUIDES, empikUrlProperties.getGuides());
        map.put(CategoryType.MOST_PRECISE_BOOK, empikUrlProperties.getMostPreciseBook());
        map.put(CategoryType.ROMANCES, empikUrlProperties.getRomances());

        return map;
    }

    private String convertEmpikPriceWithPossibleDiscountToActualPrice(String price) {
        String[] splittedElements = price.split("\\s+");
        return splittedElements[FIRST_PART_PRICE] + splittedElements[SECOND_PART_PRICE];
    }

    private String createBookURL(String title, String productID) {
        return String.format(empikUrlProperties.getConcreteBook(), title, productID);
    }

    //method is required as on empik site, sometimes occurs null for author and we need to change code for fetching
    private static String executeFetchingAuthorProcess(List<Element> siteElements, int i) {
        String author;
        Element authorElements = siteElements.get(i).select("span > a").first();
        if (authorElements != null)
            author = authorElements.ownText();
        else
            author = siteElements.get(i).select("> span > span").first().text();
        return author;
    }

    private String concatUrlWithTitle(String url, String title) {
        return String.format(url, title);
    }
}

JsoupConnector:

package bookstore.scraper.utilities;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class JSoupConnector {

    public Document connect(String url) {
        try {
            return Jsoup.connect(url).get();
        } catch (IOException e) {
            throw new IllegalArgumentException("Cannot connect to" + url);
        }
    }
}

属性类:

package bookstore.scraper.urlproperties;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Getter
@Setter
@Component
@ConfigurationProperties("external.library.url.empik")
public class EmpikUrlProperties {

    private String mostPreciseBook;
    private String bestSellers;
    private String concreteBook;
    private String romances;
    private String biographies;
    private String crime;
    private String guides;
    private String fantasy;
}

在调试测试时,我看到sources大小为0。我应该如何将模拟对象添加到源列表中,或者您能告诉我是否有更好的方法吗?

//编辑忘记粘贴测试了:P

测试

package bookstore.scraper.book;

import bookstore.scraper.book.booksource.BookServiceSource;
import bookstore.scraper.book.booksource.empik.EmpikSource;
import bookstore.scraper.book.booksource.merlin.MerlinSource;
import bookstore.scraper.dataprovider.EmpikBookProvider;
import bookstore.scraper.dataprovider.MerlinBookProvider;
import bookstore.scraper.enums.Bookstore;
import bookstore.scraper.enums.CategoryType;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import bookstore.scraper.urlproperties.MerlinUrlProperties;
import bookstore.scraper.utilities.JSoupConnector;
import org.jsoup.nodes.Document;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.List;
import java.util.Map;

import static bookstore.scraper.dataprovider.MergedBestsellersMapProvider.prepareExpectedMergedBestSellerMap;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class BookServiceTest {

    @Mock
    MerlinSource merlinSource;
    @Mock
    EmpikSource empikSource;
    @Mock
    BookServiceSource bookServiceSource;
    @Mock
    private EmpikUrlProperties empikMock;
    @Mock
    private MerlinUrlProperties merlinMock;
    @Mock
    JSoupConnector jSoupConnector;
    @Mock
    List<BookServiceSource> source;

    @InjectMocks
    BookService bookService;

    @Test
    public void getBooksByCategory() {
        List<Book> merlinBestsellers = MerlinBookProvider.prepare5Bestsellers();
        List<Book> empikBestsellers = EmpikBookProvider.prepare5Bestsellers();
        Document empikDocument = mock(Document.class);
        Document merlinDocument = mock(Document.class);

        source.add(empikSource);
        source.add(merlinSource);

        when(bookServiceSource.getName()).thenReturn(Bookstore.EMPIK);
        when(jSoupConnector.connect("https://www.empik.com/bestsellery/ksiazki")).thenReturn(empikDocument);
        when(empikMock.getBestSellers()).thenReturn("https://www.empik.com/bestsellery/ksiazki");
        when(empikSource.getBooksByCategory(CategoryType.CRIME)).thenReturn(empikBestsellers);

        when(bookServiceSource.getName()).thenReturn(Bookstore.MERLIN);
        when(jSoupConnector.connect("https://merlin.pl/bestseller/?option_80=10349074")).thenReturn(merlinDocument);
        when(merlinMock.getBestSellers()).thenReturn("https://merlin.pl/bestseller/?option_80=10349074");
        when(merlinSource.getBooksByCategory(CategoryType.CRIME)).thenReturn(merlinBestsellers);

        Map<Bookstore, List<Book>> actualMap = bookService.getBooksByCategory(CategoryType.CRIME);
        Map<Bookstore, List<Book>> expectedMap = prepareExpectedMergedBestSellerMap();

        assertEquals(expectedMap, actualMap);
    }


}
第二:

如前所述,请勿尝试模拟List对象。
通常,也避免为只能自己创建的对象创建模拟,并尝试限制自己仅模拟依赖项。

测试的简化版可能如下所示:

由于您的测试涉及的范围比Unit BookService我决定的要多得多,因此我决定在本示例中将其最小化。

您可能希望针对特定实现在测试中进行所有其他操作。

@Test
public void getBooksByCategory() {

    List<Book> empikBestsellers = EmpikBookProvider.prepare5Bestsellers();
    List<Book> merlinBestsellers = MerlinBookProvider.prepare5Bestsellers();

    BookServiceSource bookServiceSource1 = Mockito.mock(BookServiceSource.class);
    Mockito.when(bookServiceSource1.getName()).thenReturn(Bookstore.EMPIK);
    Mockito.when(bookServiceSource1.getBooksByCategory(CategoryType.CRIME)).thenReturn(empikBestsellers);   

    BookServiceSource bookServiceSource2 = Mockito.mock(BookServiceSource.class);
    Mockito.when(bookServiceSource2.getName()).thenReturn(Bookstore.MERLIN);
    Mockito.when(bookServiceSource2.getBooksByCategory(CategoryType.CRIME)).thenReturn(merlinBestsellers);      

    List<BookServiceSource> sources = new ArrayList<>();
    sources.add(bookServiceSource1);
    sources.add(bookServiceSource2);

    BookService service = new BookService(sources);
    Map<Bookstore, List<Book>> actualMap = service.getBooksByCategory(CategoryType.CRIME);

    // compare result
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章