gpt4 book ai didi

java - 从单元测试中消除 null 的方法

转载 作者:行者123 更新时间:2023-12-02 01:22:31 26 4
gpt4 key购买 nike

当尝试连接此 url 时,我在方法中收到带有 url 变量的 NPE(使用 givenTitle 解析为 null)。

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 Book getMostPreciseBook(String givenTitle) {
String concatedUrl = concatUrlWithTitle(categoryToEmpikURL.get(CategoryType.MOST_PRECISE_BOOK), givenTitle);

Document document = jSoupConnector.connect(concatedUrl);

String author = document.select("div.smartAuthorWrapper.ta-product-smartauthor").select("a").first().text();
String price = convertEmpikPriceWithPossibleDiscountToActualPrice(document.select("div.price.ta-price-tile").first().text());
String title = document.select(DIV_PRODUCT_WRAPPER).select("strong").first().text();
String productID = document.select(DIV_PRODUCT_WRAPPER).select("a").first().attr(DATA_PRODUCT_ID);
String bookUrl = createBookURL(title, productID);

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

private String concatUrlWithTitle(String url, String title) {
return String.format(url, title);
}
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;
}
}

测试:

package bookstore.scraper.book.booksource.empik;

import bookstore.scraper.book.Book;
import bookstore.scraper.enums.CategoryType;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import bookstore.scraper.utilities.JSoupConnector;
import org.jsoup.Jsoup;
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.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

import static bookstore.scraper.dataprovider.EmpikBookProvider.prepare15CrimeBooks;
import static bookstore.scraper.dataprovider.EmpikBookProvider.prepareMostPreciseBook;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class EmpikSourceTest {

@Mock
JSoupConnector jSoupConnector;
@Mock
EmpikUrlProperties empikUrlProperties;

@InjectMocks
EmpikSource empikSource;

/*@Before
public void setUp() {
when(empikUrlProperties.getConcreteBook()).thenReturn(anyString());
}
*/

@Test
public void getMostPreciseBook() throws IOException {
File in = getFile("/empik/MostPreciseBookEmpik.html");
Document empikDocument = Jsoup.parse(in, "UTF-8");

when(jSoupConnector.connect(any())).thenReturn(empikDocument);
when(empikUrlProperties.getConcreteBook()).thenReturn("https://www.empik.com/%s,%s,ksiazka-p");

Book actualBooks = empikSource.getMostPreciseBook("W pustyni i w puszczy. Lektura z opracowaniem - Henryk Sienkiewicz");
Book expectedBooks = prepareMostPreciseBook();

assertEquals(expectedBooks, actualBooks);
}

private File getFile(String resourceName) {
try {
return new File(EmpikSourceTest.class.getResource(resourceName).toURI());
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
}

}

我应该怎么做才能不让 url 为空?

堆栈跟踪:

java.lang.NullPointerException
at java.base/java.util.regex.Matcher.getTextLength(Matcher.java:1769)
at java.base/java.util.regex.Matcher.reset(Matcher.java:416)
at java.base/java.util.regex.Matcher.<init>(Matcher.java:253)
at java.base/java.util.regex.Pattern.matcher(Pattern.java:1130)
at java.base/java.util.Formatter.parse(Formatter.java:2698)
at java.base/java.util.Formatter.format(Formatter.java:2653)
at java.base/java.util.Formatter.format(Formatter.java:2607)
at java.base/java.lang.String.format(String.java:2734)
at bookstore.scraper.book.booksource.empik.EmpikSource.concatUrlWithTitle(EmpikSource.java:154)
at bookstore.scraper.book.booksource.empik.EmpikSource.getMostPreciseBook(EmpikSource.java:74)
at bookstore.scraper.book.booksource.empik.EmpikSourceTest.getMostPreciseBook(EmpikSourceTest.java:66)

EmpikUrl 属性:

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;
}

最佳答案

选项 1:

empikUrlProperties 添加新方法 getCategory

public String getCategory(CategoryType category) {
assert (category != null);

switch (category) {
case CategoryType.CRIME:
return getCrime();
case CategoryType.BESTSELLER:
return getBestSellers();
case CategoryType.BIOGRAPHY:
return getBiographies();
case CategoryType.FANTASY:
return getFantasy();
case CategoryType.GUIDES:
return getGuides();
case CategoryType.MOST_PRECISE_BOOK:
return getMostPreciseBook();
case CategoryType.ROMANCES:
return getRomances();
default:
throw new IllegalArgumentException("Unexpected category: " + category)
}
}

对于现在的测试,你只需模拟它

when(empikUrlProperties.getCategory(CategoryType.CRIME))
.thenReturn("https://www.empik.com/%s,%s,ksiazka-p");
<小时/>

选项 2:

更改构造函数以接受来自外部的 map 。这意味着您将 map 创建移至另一个类/函数,它在其他地方创建 map 。

在这种情况下,您需要手动注入(inject)模拟(不要使用@InjectMocks),因为你不应该模拟集合。

(对于这一部分,我不确定 @Autowired 将如何处理它,也许您需要将其包装到另一个类中。setter 可能是更合适的选择。)

@Autowired
public EmpikSource(EmpikUrlProperties empikUrlProperties, JSoupConnector jSoupConnector, Map<CategoryType, String> categoryToEmpikURL ) {
this.empikUrlProperties = empikUrlProperties;
this.jSoupConnector = jSoupConnector;
categoryToEmpikURL = categoryToEmpikURL;
}

关于java - 从单元测试中消除 null 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57441016/

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