AssertSubscriber not in maven dependency
Hello,
It seems that AssertSubscriber is not packaged in:
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
It is not. You should use reactor-addons. Also the class name is StepVerifier, you can look at their example here
Then the changelog is misleading: https://github.com/reactor/reactor-core/releases/tag/v3.0.3.RELEASE
if you used the test class TestSubscriber from the test sources, it has been renamed AssertSubscriber. Please note that in the future, it will be removed in favor of ScriptedSubscriber in the external project reactor-addons/reactor-tests...
Moreover, before I could use assertj to do something like:
.assertValuesWith(output -> assertThat(output).contains("foo1", "foo2"))
How can I do the equivalent with StepVerifier?
You can just use consumeNextWith and you still able to use any assert you want
StepVerifier.create(productPromotionStamp) .consumeNextWith(promotionStamp -> { assertThat(promotionStamp.getGlobalId()).isEqualTo("9200000005286435"); assertThat(promotionStamp.hasImageUrl()).isFalse(); }) .expectComplete() .verify();
@msoub good point, I will edit the release notes on github
@msoub were you able to work with StepVerifier and does the release note do a better job at pointing to it now?
Yes thank you. One feedback though: I think it's pretty error-prone that we have to explicitly verify the StepVerifier. If we forget the .verify(), no error will be thrown and nothing will be checked.
And actually I'm struggling to convert that to the new way:
@Test
public void testProcessor() {
DirectProcessor<String> processor = DirectProcessor.create();
AssertSubscriber<String> subscriber = processor.log().subscribeWith(AssertSubscriber.create());
processor.onNext("1");
processor.onNext("2");
processor.onComplete();
subscriber
.assertValues("1", "2")
.assertComplete();
}
Any suggestion?
Yes, there's the then() method that you can use to execute code during the verification:
@Test
public void testEmitter() {
DirectProcessor<String> processor = DirectProcessor.create();
StepVerifier.create(processor.log())
// 2 optional checks if you want to ensure nothing comes in
// before the processor is used
.expectSubscription()
.expectNoEvent(Duration.ofMillis(500))
// manipulate the processor during verification
.then(() -> {
processor.onNext("1");
processor.onNext("2");
processor.onComplete();
})
.expectNext("1", "2")
.expectComplete()
.verify();
}
Thank you very much.
What do you think about my remark regarding verify()?
Yeah, forgetting verify() is a possibility. The problem is that verify() step has quite a lot to deal with, plus there is a verify(Duration) timeout version... So it would be hard for us to remove that step. We'll try to keep that feedback in mind, see if we come up with an elegant solution.