How would I test a no events yet logic with from RxJava with Project Reactor Stepverifier?


How would I test a no events yet logic with from RxJava with Project Reactor Stepverifier?



I'm currently trying to figure out, how to do a similar test in Project Reactor.
Basically I want to ensure that before connect no events occur.


@Test
void connectable() {
Observable<String> provider = Observable.just("Test1", "Test2");
ConnectableObservable<String> connectable = provider.publish();
TestObserver<String> testSubscriber = connectable.test();
testSubscriber.assertEmpty();
connectable.connect();
testSubscriber.assertResult("Test1", "Test2").assertComplete();
}



This is my current attempt, but it is not correct, how would I get this to work?


@Test
void connectable() {
Flux<String> provider = Flux.just("Test1", "Test2");
ConnectableFlux<String> connectable = provider.publish();
FirstStep<String> tester = StepVerifier.create(connectable).expectNoEvent(Duration.ofMinutes(1));
connectable.connect();
tester.expectNext("Test1", "Test2").expectComplete().verify();
}




1 Answer
1



You're almost there. StepVerifier tests the sequence as a whole and you cannot add imperative assertions in the middle. But you can make your assertions and state-modifying calls within the StepVerifier! For that, use then(Runnable):


StepVerifier


StepVerifier


then(Runnable)


@Test
public void stepVerifierTestConnect() {
Flux<String> provider = Flux.just("Test1", "Test2");
ConnectableFlux<String> connectable = provider.publish();

StepVerifier.create(connectable)
.expectSubscription() //expectNoEvent counts the subscription as an event
.expectNoEvent(Duration.ofSeconds(3))
.then(connectable::connect)
.expectNext("Test1", "Test2")
.expectComplete()
.verify();
}



Note the expectSubscription first. This avoid expectNoEvent from blowing up because it considers the act of subscribing as an event (and there is still a subscription to the ConnectableFlux itself - it just prevents subscription to its own upstream until you call connect()).


expectSubscription


expectNoEvent


ConnectableFlux


connect()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift