我正在尝试为名为 getBestSellers()
的方法编写单元测试。
这里是:
package bookstore.scraper.book.scrapingtypeservice;
import bookstore.scraper.enums.Bookstore;
import bookstore.scraper.book.Book;
import bookstore.scraper.fetcher.empik.EmpikFetchingBookService;
import bookstore.scraper.fetcher.merlin.MerlinFetchingBookService;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import bookstore.scraper.urlproperties.MerlinUrlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import static bookstore.scraper.utilities.JSoupConnector.connect;
@Service
public class BestSellersService {
private final EmpikUrlProperties empikUrlProperties;
private final MerlinUrlProperties merlinUrlProperties;
private final EmpikFetchingBookService empikBookService;
private final MerlinFetchingBookService merlinBookService;
@Autowired
public BestSellersService(EmpikFetchingBookService empikBookService, MerlinFetchingBookService merlinBookService, EmpikUrlProperties empikUrlProperties, MerlinUrlProperties merlinUrlProperties) {
this.empikBookService = empikBookService;
this.merlinBookService = merlinBookService;
this.empikUrlProperties = empikUrlProperties;
this.merlinUrlProperties = merlinUrlProperties;
}
public Map<Bookstore, List<Book>> getBestSellers() {
Map<Bookstore, List<Book>> bookstoreWithBestSellers = new EnumMap<>(Bookstore.class);
bookstoreWithBestSellers.put(Bookstore.EMPIK, empikBookService
.get5BestSellersEmpik(connect(empikUrlProperties.getEmpik().getBestSellers())));
bookstoreWithBestSellers.put(Bookstore.MERLIN, merlinBookService
.get5BestSellersMerlin(connect(merlinUrlProperties.getMerlin().getBestSellers())));
return bookstoreWithBestSellers;
}
}
所以,首先我准备了如下所示的测试:
package bookstore.scraper.book.scrapingtypeservice;
import bookstore.scraper.book.Book;
import bookstore.scraper.dataprovider.EmpikBookProvider;
import bookstore.scraper.dataprovider.MerlinBookProvider;
import bookstore.scraper.enums.Bookstore;
import bookstore.scraper.fetcher.empik.EmpikFetchingBookService;
import bookstore.scraper.fetcher.merlin.MerlinFetchingBookService;
import bookstore.scraper.urlproperties.EmpikUrlProperties;
import bookstore.scraper.urlproperties.MerlinUrlProperties;
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 org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class BestSellersServiceTest {
@Mock
private EmpikFetchingBookService empikBookService;
@Mock
private MerlinFetchingBookService merlinBookService;
@Mock
private EmpikUrlProperties empikUrlProperties;
@Mock
private MerlinUrlProperties merlinUrlProperties;
@InjectMocks
private BestSellersService bestSellersService;
@Test
public void getBestSellers() {
List<Book> merlinBestsellers = EmpikBookProvider.prepare5Bestsellers();
List<Book> empikBestsellers = MerlinBookProvider.prepare5Bestsellers();
when(empikBookService.get5BestSellersEmpik(any())).thenReturn(empikBestsellers);
when(merlinBookService.get5BestSellersMerlin(any())).thenReturn(merlinBestsellers);
//when(empikUrlProperties.getEmpik().getBestSellers()).thenReturn(anyString());
//when(merlinUrlProperties.getMerlin().getBestSellers()).thenReturn(anyString());
Map<Bookstore, List<Book>> actualMap = bestSellersService.getBestSellers();
Map<Bookstore, List<Book>> expectedMap = null;
assertEquals(expectedMap, actualMap);
assertThat(actualMap).hasSize(expectedMap.size());
}
}
没有设置属性类的行为,因为我认为这是不必要的,因为我在调用 empikBookService.get5BestSellersEmpik
时放置了 any()
(与 merlinBookService
相同) code>) 但调用时抛出 NPE
bookstoreWithBestSellers.put(Bookstore.EMPIK, empikBookService
.get5BestSellersEmpik(connect(empikUrlProperties.getEmpik().getBestSellers())));
我已经调试过它并且我已经看到了
empikUrlProperties.getEmpik().getBestSellers()))
给了我 NPE。 所以我设置这样的行为:
when(empikUrlProperties.getEmpik().getBestSellers()).thenReturn(anyString());
when(merlinUrlProperties.getMerlin().getBestSellers()).thenReturn(anyString());
现在它给我提供了带有 stactrace 的 NPE:
ava.lang.NullPointerException
at bookstore.scraper.book.scrapingtypeservice.BestSellersServiceTest.getBestSellers(BestSellersServiceTest.java:48)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
.
.
.
测试方法中使用的connect
方法:
@UtilityClass
public class JSoupConnector {
public static Document connect(String url) {
try {
return Jsoup.connect(url).get();
} catch (IOException e) {
throw new IllegalArgumentException("Cannot connect to" + url);
}
}
}
属性类(与 merlin 相同)
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")
public class EmpikUrlProperties {
private Empik empik = new Empik();
@Getter
@Setter
public static class Empik {
private String mostPreciseBook;
private String bestSellers;
private String concreteBook;
private String romances;
private String biographies;
private String crime;
private String guides;
private String fantasy;
}
}
我做错了什么?为什么当我输入 any()
最佳答案
存在很多问题,但最主要的问题是您误解了模拟、参数求值和 any()
的工作原理。
您正在使用
when(empikBookService.get5BestSellersEmpik(any())).thenReturn(empikBestsellers);
这告诉模拟 empikBookService 每当调用其 get5BestSellersEmpik
方法时,它都应该返回 empikBestsellers
,无论传递给该方法的参数是什么。
执行测试时,您的实际代码作为参数传递什么?它传递返回的值
connect(empikUrlProperties.getEmpik().getBestSellers())
关键部分是首先计算此表达式,然后将其结果作为参数传递给 get5BestSellersEmpik()
方法。
就像你做的那样
System.out.println(a + b)
首先评估a + b
。如果结果是 42,则将值 42 传递给 println(),然后 println 打印 42。
因此,为了使您的测试不失败,表达式
connect(empikUrlProperties.getEmpik().getBestSellers())
必须评估成功。它的结果并不重要,因为您已经将模拟配置为接受任何参数。但这无关紧要。
你正在尝试做
when(empikUrlProperties.getEmpik().getBestSellers()).thenReturn(anyString());
这没有任何意义。
首先,因为 empikUrlProperties.getEmpik()
将返回 null,因为 empikUrlProperties
是一个模拟,并且模拟默认返回 null。并且 null.getBestSellers()
将导致 NullPointerException。
第二,因为告诉模拟它应该返回任何字符串没有意义。如果您不关心它应该返回的字符串,那么您可以自己选择一个字符串,并让它返回该字符串。 anyString()
实际上返回 null,因此您告诉它返回 null。
所以你需要解决这个问题。始终思考您的代码在做什么,而不是尝试应用配方。
最后,您的测试还调用 connect(...)
,这是一个静态方法,您没有(也不能)模拟它。该方法也会被调用。它会尝试连接到实际的 URL。因此,如果在对该 URL 进行测试期间没有任何响应,则该方法也不起作用。这个 connect()
方法确实应该是您的服务依赖项的一部分,并且应该模拟该依赖项。
关于java - 单元测试时的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57336935/