【问题标题】:How do I use Reactor's StepVerifier to verify a Mono is empty?如何使用 Reactor 的 StepVerifier 来验证 Mono 是否为空?
【发布时间】:2018-12-26 23:49:08
【问题描述】:

我正在使用StepVerifier 来测试值:

@Test
public void testStuff() {
    Thing thing = new Thing();
    Mono<Thing> result = Mono.just(thing);
    StepVerifier.create(result).consumeNextWith(r -> {
        assertEquals(thing, r);
    }).verifyComplete();
}

我现在想做的是测试 Mono 中是否缺少项目。像这样:

@Test
public void testNoStuff() {
    Mono<Thing> result = Mono.empty();
    StepVerifier.create(result)... // what goes here?
}

我想测试 Mono 实际上是空的。我该怎么做?

【问题讨论】:

    标签: java unit-testing reactive-programming project-reactor


    【解决方案1】:

    只需使用verifyComplete()。如果Mono 发出任何数据,它将使stepverifier 失败,因为此时它不期望onNext 信号。

    【讨论】:

      【解决方案2】:

      这里检查onNext没有被调用

       StepVerifier.create(result).expectNextCount(0).verifyComplete()
      

      【讨论】:

      • 请解释与另一个较旧的赞成答案的区别。看来你推荐的是同一个东西,只是没有任何解释
      猜你喜欢
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2022-11-24
      • 2021-12-15
      相关资源
      最近更新 更多