title: Need help on writing a junit test for a function which is returning Flux stream as output
body: <p>I need help in writing unit test for a function that is returning Flux stream as output.</p>
  
  <p>I have tried using the StepVerifier but I guess I am using it in the wrong way.</p>
  
  <p>In need to test the following function.</p>
  
  <pre><code>public Flux&lt;List&lt;String&gt;&gt; streamCompletedScansAfterLastSubmit(@PathVariable("username") String username) {
      Flux&lt;Long&gt; interval = Flux.interval(Duration.ofSeconds(5));
      Flux&lt;List&lt;String&gt;&gt; completeScans = Flux.fromStream(Stream.generate(() -&gt; scanService.getCompletedScansAfterLastSubmitForUser(username)));
      return Flux.zip(interval, completeScans).map(Tuple2::getT2);
    }
  </code></pre>
  
  <p>What I have tried is</p>
  
  <pre><code>  public void shouldPublishTheAssessmentStatusOnceFinished() {
      when(scanService.getCompletedScansAfterLastSubmitForUser(Mockito.anyString())).thenReturn(Arrays.asList("Scan1:Success"));
      StepVerifier.create(apiScanController.streamCompletedScansAfterLastSubmit("quays_ka"))
        .expectSubscription()
        .expectNext(Arrays.asList("Scan1:Success"))
        .verifyComplete();
    }
  </code></pre>
  
  <p>I am getting error:</p>
  
  <p>java.lang.AssertionError: expectation "expectComplete" failed (expected: onComplete(); actual: onNext([Scan1:Success]))</p>
  
  <p>Somehow the service method gets called twice (I checked that using doAnswer)</p>