Mockito带有delete方法,它在spring-webflux中发出实体实体对象



我正在尝试使用带有Spring webflux的mockito运行junit。这里的delete方法返回Mono数据,并根据下面的代码使用thenReturn发出实体对象。这里的问题是,当我试图模拟这个对象时,它只使用Mono,并在下一行给出null指针。我得到了空指针,如实际代码中所示。如何在mockito中为delete repository方法返回实体对象。实际代码

Mono<SuccessResponse> method1(RequestDto dto){
return repo.findById(dto.getId())
.flatMap(m -> {
Mono<ABC> ref= repo.delete(m).thenReturn(m);
return ref.flatMap(f -> {
if (f.getId() != 0) // Null pointer 
return Mono.just(new SuccessResponse());
}
}
}

Junit测试-我试过模拟,但都不起作用。

//Mockito.when(repo.delete(CommonUtil.ABC())).thenReturn(Mono.empty());
//Mockito.doReturn(Mono.just(CommonUtil.ABC())).when(repo).delete(service.method1());
//Mockito.when(CommonUtil.ABC().getId()).thenReturn(7L);

在我看来,你需要"告诉";您的mock返回提供给delete()的对象,如下所示:

Mockito.when(repo.delete(any())).then(returnsFirstArg());

最新更新