Как издеваться над Feign Client - PullRequest
0 голосов
/ 18 марта 2019

Я не могу издеваться над своим Feign Client, используя Mockito.

MyService.class

@Service
@AllArgsConstructor
public class MyService implements IMyService{

    private static final Logger LOGGER = LoggerFactory.getLogger(MyService .class);

    private final MyRepository repository;

    private final MyFeignClient myFeignClient;

    @Autowired
    private MyDao myDao;

    @Override
    @Async
    public void process(Map<UUID, Long> command) {
        DocIds docIds = getDocIds(command.values().stream().findFirst().get());
        archiveData(command.keySet().stream().findFirst().get(), documentIds.getIds());
    }

    private DocumentIds getDocIds(Long retentionId) {
        return myFeignClient.getDocumentIds(retentionId);
    }

private void archiveData(UUID execId, List<Long> docIds) {
    List<MyDocument> myDocs= prepareMyDocs(docIds);
    repository.saveAll(myDocs);
}

И мой тестовый класс:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class ArchiveServiceTest {

    @Autowired
    ArchiveService archiveService;

    @MockBean
    MyDao myDao;

    @MockBean
    DocRepository archiveRepository;

    @MockBean
    private MyFeignClient myFeignClient;


    @Test
    public void shouldReturnTheSameNumberOfDocumentsToArchive() {
        //given
        List<DocData> documentData = prepareDocumentData();
//      doReturn(new DocIds()).when(myFeignClient).getDocumentIds(1000L);
        DocumentIds documentIds = new DocumentIds();
        documentIds.setIds(Arrays.asList(1L, 2L));
        when(myFeignClient.getDocIds(any())).thenReturn(documentIds);
        when(documentDataDao.getAllDocumentData(anyList())).thenReturn(documentData);
        doNothing().when(archiveRepository.saveAll(any()));

        //when
        Map<UUID, Long> command = new HashMap<>();
        command.put(UUID.randomUUID(), 1000L);

        archiveService.process(command);

        //then
        ...

MyFeignClient:

@FeignClient(name = "myFeignClient", url = "${feign.searcher.path}")
public interface MyFeignClient{

    @RequestMapping(method = RequestMethod.GET, path = "/document/path/{id}")
    DocIds getDocumentIds(@PathVariable("id") Long id);

}

При запуске теста,

return myFeignClient.getDocumentIds(retentionId);

возвращает NULL. Зачем? У меня нет больше идей. Я не хочу использовать WireMock. То же самое происходит с моим documentDataDao, который не возвращает никаких значений (null), указанных в предложении thenReturn ().

...