gpt4 book ai didi

java - 将元素添加到模拟列表

转载 作者:行者123 更新时间:2023-11-30 05:31:34 24 4
gpt4 key购买 nike

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

@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 是一个接口(interface)。该接口(interface)由两个类实现。我只会提供一个,因为第二个非常相似。

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
}

关于java - 将元素添加到模拟列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57419843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com