我无法使用Mockito嘲笑Feign Client。
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也会发生同样的情况,它不返回thenReturn()子句中指定的任何值(空)。
答案 0 :(得分:0)
您是否尝试过这种方式?
Mockito.when(myFeignClient.getDocumentIds(Mockito.eq(1000L))).thenReturn(new DocIds());
在您的示例中,模拟已被注释掉;)
// doReturn(new DocIds()).when(myFeignClient).getDocumentIds(1000L);
但是我确定这只是您示例中的错误。