java - 单元测试中私有方法返回的值
问题描述
我的服务中有类似的方法,如下所示:
public List<ProductDTO> findById(final int id) {
final List<String> categoryList = getCategoryById(id);
final List<UUID> productUuidList =
productRepository.findAllUuidByCategoryIdIn(categoryList);
return getProductDTOList();
}
private static List<String> getCategoryById(final int id) {
// code omitted
return ...
}
我有一个单元测试findById
方法,如下所示。
@Test
public void test_findByZoneOffset() {
List<String> categoryList = new ArrayList<>();
// code omitted (fill categoryList with proper values)
final List<UUID> productUuidList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
productUuidList.add(UUID.randomUUID());
}
when(productRepository.findAllUuidByCategoryIdIn(categoryList))
.thenReturn(productUuidList);
// code omitted
final List<ProductDTO> productDTOList = productService.findById(5);
}
但是,我不能创建categoryList
与方法返回的相同值,getCategoryById()
因为它是私有方法。另一方面,由于该私有方法中没有服务或存储库调用,我不想从单元测试方法中调用它,只想创建一个模拟categoryList
和 make categoryList
infindById
方法是相同的。那么,我该怎么做呢?
解决方案
推荐阅读
- python - 即使在删除错误行后也会出现 django 错误
- linux - 显示在后台启动的进程的 shell 脚本
- go - 无论对象类型如何,如何拆分对象?
- c++ - 在 OpenCV 中与屏幕捕获一起使用的函数 matchTemplate
- javascript - 如何防止带有网络扩展的历史条目?
- python - 来自修改后的 Google 提供的模板的 Cloud Dataflow 任务中的自动缩放性能问题
- android - 如何将自定义坐标传递给地图框导航启动器?
- logstash-grok - Grok 表达式解析日志数据
- c - 在 C 中为多个进程之间的进程间通信创建管道
- vim - 如何添加键盘快捷键来激活/禁用 VSCode 扩展?