【发布时间】:2021-08-24 20:11:33
【问题描述】:
我想检查是否在方法执行期间调用了 toDishResponseDTO。但这是不可能的,因为这是正在测试的类的方法。如何做到这一点?
类
@ExtendWith(MockitoExtension.class)
class DishServiceTest {
@Mock
DishRepository dishRepository;
@Mock
RestaurantRepository restaurantRepository;
@Autowired
@InjectMocks
DishService dishService;
待测方法
public List<DishResponseDTO> getAll() {
List<Dish> dishlsit = dishRepository.findAll();
return dishlsit.stream()
.map(this::toDishResponseDTO)
.collect(Collectors.toList());
}
测试
@Test
void shouldCallFindAllReturnDto_whenV() {
Mockito.when(dishRepository.findAll()).thenReturn(TestData.ENTITY_DISH);
dishService.getAll();
Mockito.verify(dishRepository, times(1)).findAll();
Mockito.verify(dishService times(6)).toDishResponseDTO(any()); // compile error, because verify can be called only on mocks
}
【问题讨论】: