Mockito无法模拟FlatMap RxJava中的代码



我有一个代码块与flatMap,我试图测试,但似乎Mockito不去flatMap内。

这是我的实际代码类code .java


private final ObjectResolver objectResolver;
public Mono<String> function toTest(Object obj) {
return Mono.just(obj)
.flatMap(obj -> objectResolver.resolve(obj.getField())
.map(resolved -> StringUtils.join(resolved.getField(), obj.getAnotherField())))
}

下面是我测试它的方法:

@InjectMocks
private Code code
@Mock
private ObjectResolver objectResolver
@Test
void testToTest() {
when(objectResolver.resolve(any()).thenReturn(Mono.just(new ResolvedObject()));
StepVerifier.create(code.toTest(new Object()))
.expectNext("resultString")
.verifyComplete();
}

当我尝试调试时,Mockito似乎不在flatMap内。什么好主意吗?

您需要订阅mono来发布项目。

@Test
void testToTest() {
when(objectResolver.resolve(any()).thenReturn(Mono.just(new ResolvedObject()));
StepVerifier.create(code.toTest(new Object()))
.expectNext("resultString")
.verifyComplete();

Mono<String> returnedMono = code.toTest(new Object());
returnedMono.subscribe();
}

相关内容

  • 没有找到相关文章

最新更新