Dealing with virtual time

Despite the fact that the essence of testing is in covering business logic, there is another very important part that should be given thought as well. To understand this characteristic, we should consider the following example of the code first:

public Flux<String> sendWithInterval() {
   return Flux.interval(Duration.ofMinutes(1))
      .zipWith(Flux.just("a", "b", "c"))
      .map(Tuple2::getT2);
}

This example shows a naive way of publishing events with a specific interval. In a real-world scenario, behind the same API, there might be a more complex mechanism hidden that involves long delays, timeouts, and event intervals. To verify such code using StepVerifier, we might end up with the following test case:

StepVerifier .create(sendWithInterval()) ...

Get Hands-On Reactive Programming in Spring 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.