我的服务层有这样的方法:
public Long calculateItemsCostInShoppingCart(Long shoppingCartId) {
List<Item> items = shoppingCartRepository.findAllItems(shoppingCartId);
Long cost = 0L;
for (Item item : items) {
cost += item.getPrice();
}
return cost;
}
而且我需要测试列表中所有项目的汇总成本的计算。我考虑过 mockito,但没有成功,因为 mockito 只是创建 stub ,我需要真正的入口数据和基于它们的结果。怎么做到的?
最佳答案
// create mock
ShoppingRepository mock = mock(ShoppingRepository.class);
// define return value for method findAllItems()
when(mock.findAllItems()).thenReturn(listOf(...));
关于java - 如何在方法中测试业务逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490612/