title: JUnit 5 @WebFluxTest test to a Flux @PostMapping using WebTestClient
body: <p>I'm trying out Spring WebFlux using annotation controller and embedded mongodb (JDK11). I am trying to write <code>@WebFluxTest</code> for my <code>@RestController</code>, in particular, <code>@PostMapping</code> portion.</p>
  
  <p>It's a just basic setup with <code>@RestController</code>, and <code>@Repository</code>.</p>
  
  <p>In my <code>@RestController</code>:</p>
  
  <pre><code>@RestController
  @RequestMapping("/address")
  @AllArgsConstructor
  public class AddressController {
  
      private final AddressRepository addressRepository;
  
      @ResponseStatus(HttpStatus.CREATED)
      @PostMapping(value = "/create")
      public Flux&lt;Address&gt; create(@RequestBody Flux&lt;Address&gt; address) {
          return address.flatMap(this.addressRepository::save);
      }
  }
  </code></pre>
  
  <p>In my test:</p>
  
  <pre><code>@ExtendWith(SpringExtension.class)
  @WebFluxTest(AddressController.class)
  public class AddressControllerTest {
  
      @Autowired
      private WebTestClient webTestClient;
  
      @MockBean
      private AddressRepository addressRepository;
  
      @Test
      @DisplayName("Create")
      public void create() {
  
          Flux&lt;Address&gt; addresses = Flux.just("Hello").map(add -&gt; 
                              Address.builder().id("5bf907ff2e21d401d4054a4d").street(add).build());
  
          Address add = Address.builder().id("5bf907ff2e21d401d4054a4d").street("Hello").build();
  
          when(this.addressRepository.saveAll(addresses)).thenReturn(addresses);
  
          this.webTestClient
              .post()
              .uri("/address/create")
              .body(addresses, Address.class)
              .exchange()
              .expectStatus().isCreated()
              .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
              .expectBody(Address.class).isEqualTo(add);
      }
  }
  </code></pre>
  
  <p>And a very simple POJO:</p>
  
  <pre><code>@Data
  @Builder
  @Document
  @AllArgsConstructor
  @NoArgsConstructor
  public class Address {
  
      @Id
      private String id;
      private String street;
  }
  </code></pre>
  
  <p>Question:</p>
  
  <p>I want to be able to create the endpoint to allow creation of <code>0..*</code> address. </p>
  
  <p>How to correctly write the unit test so that I can <code>mock</code> the call in <code>create()</code>? If I do </p>
  
  <pre><code>when(this.addressRepository.save(add)).thenReturn(addresses);
  </code></pre>
  
  <p>then, it's a <code>Mono</code></p>
  
  <p>But if I do</p>
  
  <pre><code>when(this.addressRepository.saveAll(addresses)).thenReturn(addresses);
  </code></pre>
  
  <p>then it's not <code>mocking</code> the correct method call in <code>create()</code>.</p>
  
  <p>What is the correct way to write this unit test?</p>
  
  <p>Thanks! If I didn't provide enough info, please let me know.</p>
  
  <p>Update: I am able to <code>create</code> using <code>Postman</code> to my <code>/address/create</code> endpoint though.</p>