我正在尝试对负责添加到地图分类书籍的方法进行单元测试。
@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是一个接口。该接口由两个类实现。我只提供一个,因为第二个非常相似。
Jsoup连接器:
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。我应该如何将模拟对象添加到源列表中,或者您能告诉我是否有更好的方法吗?
不负相思意
繁花如伊
鸿蒙传说
相关分类