2

When I write tests that verify reactive components I usually use the StepVerifier with the following pattern:

StepVerifier.create(...)
  .assertNext(...)
  ...
  .verifyComplete();

This approach works nice but sometimes it is necessary to grab the produced value and continue the test scenario using it. In this case I have to switch to something like:

var producedValue = publisher.block();
assertThat(producedValue)...;
someOtherAction(producedValue);

The latter is completely fine and works as expected however I feel that I might miss something and StepVerifier API should have an option for doing this as well.

Is there any way to avoid blocking in such scenario and still stick to the StepVerifier?

fyrkov
  • 2,245
  • 16
  • 41

1 Answers1

1

Could you use the consumeNextWith operator to grab the value without using block?

        StepVerifier.create(...)
            .consumeNextWith(producedValue -> {
                someOtherAction(producedValue);
                assertThat(producedValue);
                // if the next action produces a Mono
                //StepVerifier.create(someOtherReactiveAction(producedValue))
                //        .expectNext(result)
                //        .verifyComplete();
            })
            .verifyComplete();
Michael McFadyen
  • 2,675
  • 11
  • 25
  • The part of the answer in comment does not work. Using StepVerifier.create().expectNext().verifyComplete() inside consumeNextWith() makes the test run forever – aminits Jun 28 '22 at 05:33
  • @aminits I would have to disagree and have verified this with a simple example. Maybe your stream is never completing but I can not really tell without seeing your test. Maybe raise a separate question detailing the issue you are encountering. – Michael McFadyen Jun 29 '22 at 20:13