title: Global exception handling in spring webflux during tests
body: <p>I'm developing a service with spring webflux. I implemented exception handling using <code>@ControllerAdvice</code>. It works pretty well, but when I run integration tests it seems that <code>@ControllerAdvice</code> annotated component is not loaded, resulting in this response:</p>
  
  <pre><code>{
     "timestamp":"2019-11-28T08:56:47.285+0000",
     "path":"/fooController/bar",
     "status":500,
     "error":"Internal Server Error",
     "message":"java.lang.IllegalStateException: Could not resolve parameter [1] in protected org.springframework.http.ResponseEntity&lt;it.test.model.Response&gt; it.test.exception.ExceptionHandlerController.handleServiceException(java.lang.Exception,org.springframework.web.context.request.WebRequest): No suitable resolver
  }
  </code></pre>
  
  <p>This is my controller advice:</p>
  
  <pre><code>@ControllerAdvice
  public class ExceptionHandlerController extends ResponseEntityExceptionHandler {
  
      private final Logger logger = LoggerFactory.getLogger(ExceptionHandlerController.class);
  
      @ExceptionHandler
      protected ResponseEntity&lt;Response&gt; handleServiceException(Exception ex, WebRequest request) {
  
          this.logger.error("Error occurred: \"{}\"", ex.getMessage());
  
          Response&lt;Foo&gt; response = new Response&lt;&gt;(new Foo(),
                  "generic error",
                  HttpStatus.INTERNAL_SERVER_ERROR);
  
          return new ResponseEntity&lt;&gt;(response, null, HttpStatus.OK);
      }
  }
  </code></pre>
  
  <p>And this is my integration test class</p>
  
  <pre><code>@ExtendWith(SpringExtension.class)
  @WebFluxTest(MyController.class)
  @ContextConfiguration(classes = {MyController.class, MyServiceImpl.class, ExceptionHandlerController.class })
  public class MyControllerIT {
  
      @Autowired
      private WebTestClient webTestClient;
  
      @Test
      public void testShouldFail() throws IOException {
  
          return this.webTestClient.get()
              .uri(uri)
              .accept(MediaType.APPLICATION_JSON)
              .exchange()
              .expectStatus().isOk()
              .expectBody()
              .jsonPath("$.statusCode").isEqualTo(500);
      }
  }
  </code></pre>