title: Spring-Webflux: Handler function unit test is throwing UnsupportedMediaTypeStatusException
body: <p>I am trying to write Unit test to the handler function, I followed the <a href="https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/ResourceHandlerFunctionTests.java" rel="nofollow noreferrer">example</a> from the Spring project. Can someone help me why the following test is throwing <code>UnsupportedMediaTypeStatusException</code>?</p>
  
  <p>Thanks</p>
  
  <p>Handler function</p>
  
  <pre class="lang-java prettyprint-override"><code>public Mono&lt;ServerResponse&gt; handle(ServerRequest serverRequest) {
          log.info("{} Processing create request", serverRequest.exchange().getLogPrefix());
  
          return ok().body(serverRequest.bodyToMono(Person.class).map(p -&gt; p.toBuilder().id(UUID.randomUUID().toString()).build()), Person.class);
      }
  </code></pre>
  
  <p>Test Class</p>
  
  <pre class="lang-java prettyprint-override"><code>@SpringBootTest
  @RunWith(SpringRunner.class)
  public class MyHandlerTest {
  
      @Autowired
      private MyHandler myHandler;
      private ServerResponse.Context context;
  
      @Before
      public void createContext() {
          HandlerStrategies strategies = HandlerStrategies.withDefaults();
          context = new ServerResponse.Context() {
              @Override
              public List&lt;HttpMessageWriter&lt;?&gt;&gt; messageWriters() {
                  return strategies.messageWriters();
              }
  
              @Override
              public List&lt;ViewResolver&gt; viewResolvers() {
                  return strategies.viewResolvers();
              }
          };
      }
  
      @Test
      public void handle() {
  
          Gson gson = new Gson();
  
          MockServerWebExchange exchange = MockServerWebExchange.from(
                  MockServerHttpRequest.post("/api/create")
                          .body(gson.toJson(Person.builder().firstName("Jon").lastName("Doe").build())));
  
          MockServerHttpResponse mockResponse = exchange.getResponse();
  
          ServerRequest serverRequest = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders());
  
          Mono&lt;ServerResponse&gt; serverResponseMono = myHandler.handle(serverRequest);
  
          Mono&lt;Void&gt; voidMono = serverResponseMono.flatMap(response -&gt; {
              assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
              boolean condition = response instanceof EntityResponse;
              assertThat(condition).isTrue();
              return response.writeTo(exchange, context);
          });
  
          StepVerifier.create(voidMono)
                  .expectComplete().verify();
  
          StepVerifier.create(mockResponse.getBody())
                  .consumeNextWith(a -&gt; System.out.println(a))
                  .expectComplete().verify();
  
          assertThat(mockResponse.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
  
      }
  }
  </code></pre>
  
  <p>Error Message: 
  java.lang.AssertionError: expectation "expectComplete" failed (expected: onComplete(); actual: onError(org.springframework.web.server.UnsupportedMediaTypeStatusException: 415 UNSUPPORTED_MEDIA_TYPE "Content type 'application/octet-stream' not supported for bodyType=com.example.demo.Person"))</p>