title: Testing Mono and Flux using Mockito
body: <p>Im currently writing some basic unit tests for my REST-Endpoints. I use <a href="http://site.mockito.org/" rel="nofollow noreferrer">Mockito</a> for that. Here one example:</p>
  
  <pre><code>@MockBean
  private MyService service;
  
  @Test
  public void getItems() {
      Flux&lt;Item&gt; result = Flux.create(sink -&gt; {
          sink.next(new Item("1"));
          sink.next(new Item("2"));
          sink.complete();
      });
  
      Mono&lt;ItemParams&gt; params = Mono.just(new ItemParams("1"));
  
      Mockito.when(this.service.getItems(params)).thenReturn(result);
  
      this.webClient.post().uri("/items")
              .accept(MediaType.APPLICATION_STREAM_JSON)
              .contentType(MediaType.APPLICATION_STREAM_JSON)
              .body(BodyInserters.fromPublisher(params, ItemParams.class))
              .exchange()
              .expectStatus().isOk()
              .expectBodyList(Item.class).isEqualTo(Objects.requireNonNull(result.collectList().block()));
  }
  </code></pre>
  
  <p>This implementation leads to the following error:</p>
  
  <pre><code>java.lang.AssertionError: Response body expected:&lt;[Item(name=1), Item(name=2)]&gt; but was:&lt;[]&gt;
  
  &gt; POST /items
  &gt; WebTestClient-Request-Id: [1]
  &gt; Accept: [application/stream+json]
  &gt; Content-Type: [application/stream+json]
  
  Content not available yet
  
  &lt; 200 OK
  &lt; Content-Type: [application/stream+json;charset=UTF-8]
  
  No content
  </code></pre>
  
  <p>When I exchange the parameter in the Mockito Statement with <code>Mockito.any()</code></p>
  
  <pre><code>Mockito.when(this.service.getItems(Mockito.any())).thenReturn(result);
  </code></pre>
  
  <p>The test runs through successfully.
  That means that for some reason the <code>params</code> I put into the <code>Mockito</code> Statement isnt equal to the <code>params</code> object which I put into <code>BodyInserters.fromPublisher(params, ItemParams.class)</code></p>
  
  <p>How am I supposed to test my functionality then?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>REST-Endpoint</p>
  
  <pre><code>@PostMapping(path = "/items", consumes = MediaType.APPLICATION_STREAM_JSON_VALUE, produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
  public Flux&lt;Item&gt; getItems(@Valid @RequestBody Mono&lt;ItemParams&gt; itemParms) {
      return this.service.getItems(itemParms);
  }
  </code></pre>