1

Метод repositoryAuto.findAll(specification, pageable) возвращает Page<Auto> и у меня получилось замокать эту часть c помощью кода:

when(repositoryAuto.findAll(any(), (Pageable) any())).thenReturn(getAutoEntityPage());

Но в сервисе сам код выглядит так в одну строчку :

Page<AutoDTO> dtos = repository.findAll(specification, pageable).map(autoMapper::toShortDto);

Как можно замокать метод .map интерфейса Page ,чтобы он возращал Page<AutoDTO>?

1 ответ 1

1

Можно так:

Page<Auto> getAutoEntityPage(Page<AutoDTO> dtoPage) {
  Page<Auto> autoPageMock = mock(Page.class);
  // не уверен, но возможно и .map(autoMapper::toShortDto) сработает, нужно проверить
  when(autoPageMock.map(any())).thenReturn(dtoPage); 
  
  return autoPageMock;
}


// передаем объект 
Page<AutoDTO> dtoPage = ...;
when(repositoryAuto.findAll(any(), (Pageable) any()))
   .thenReturn(getAutoEntityPage(dtoPage));

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.