如何使用提供 Mono 未完成的 StepVerifier 进行验证? [英] How to verify with StepVerifier that provided Mono did not completed?

查看:35
本文介绍了如何使用提供 Mono 未完成的 StepVerifier 进行验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 StepVerifier 可以很容易地检查提供的 Mono 是否已经完成(只需通过 StepVerifier<中的 expectComplete() 方法)/code>),但是如果需要检查相反的情况我该怎么办?

With StepVerifier it is very easy to check whether provided Mono has completed (just by expectComplete() method in StepVerifier), but what should I do if need to check the opposite case ?

我尝试使用这种方法:

    @Test
    public void neverMonoTest() {
        Mono<String> neverMono = Mono.never();
        StepVerifier.create(neverMono)
            .expectSubscription()
            .expectNoEvent(Duration.ofSeconds(1))
            .thenCancel()
            .verify();
    }

这样的测试就通过了.但这是误报,因为当我用 Mono.empty() 替换 Mono.never() 时,测试仍然是绿色的.

and such test passes. But this is false positive, because when I replace Mono.never() with Mono.empty() the test is still green.

有没有更好更可靠的方法来检查 Mono 是否完成(当然在给定的时间范围内)?

Is there any better and reliable method to check lack of Mono's completion (of course within given scope of time) ?

推荐答案

看起来您遇到了 reactor-test 中的错误,不幸的是,一个看起来不会很快得到解决的错误:

It looks like you're hitting a bug in reactor-test, and unfortunately one that doesn't look to be solved any time soon:

由于我的记忆,这是反应堆测试设计中的一个持续缺陷.一旦反应堆测试从头/显着重写,这很可能会得到修复.

Due to my memory that was a constant flaw in design of reactor-test. Most likely that will be fixed once reactor-test will be rewritten from scratch / significantly.

降级到 3.1.2 似乎解决了这个问题,但这是一个相当大的降级.我知道的唯一其他解决方法是 由 PyvesB 在这里发布,并涉及等待 Mono 超时:

Downgrading to 3.1.2 seems to fix the problem, but that's quite a downgrade. The only other workaround I'm aware of was posted by PyvesB here, and involves waiting for the Mono to timeout:

Mono<String> mono = Mono.never();
StepVerifier.create(mono.timeout(Duration.ofSeconds(1L)))
        .verifyError(TimeoutException.class);

当下一个版本推出时,你应该能够做到:

When the next release rolls out, then you should be able to do:

Mono<String> mono = Mono.never();
StepVerifier.create(mono)
        .expectTimeout(Duration.ofSeconds(1));

...作为更简洁的替代方案.

...as a more concise alternative.

这篇关于如何使用提供 Mono 未完成的 StepVerifier 进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆