我正在尝试为名为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());
}
}
无需设置属性类的行为,因为我认为这是不必要的,因为调用any()
时放了empikBookService.get5BestSellersEmpik
(与merlinBookService
相同),但是调用
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());
现在它给了我带触角的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()
答案 0 :(得分:1)
设置模拟时:
when(empikUrlProperties.getEmpik().getBestSellers()).thenReturn(anyString());
您嘲笑了empikUrlProperties
,这很好,但是您没有告诉嘲笑当调用getEmpik()
时该怎么做。因此,该方法调用将在测试中以及生产代码中(在此行之前)都返回null,因此这是在调用getBestSellers()
时导致NPE的原因。
因此,将其设置为模拟,例如:
@Mock
private EmpikUrlProperties empikUrlProperties;
@Mock
private EmpikUrlProperties.Empik empikMock;
when(empikUrlProperties.getEmpik()).thenReturn(empikMock);
when(empikMock.getBestSellers()).thenReturn(anyString());
答案 1 :(得分:1)
有很多问题,但主要的问题是您误解了模拟,参数求值和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()
方法实际上应该是您服务的依赖项的一部分,并且应该模拟该依赖项。