0
WebClient
  .builder()
  .build()
  .post()
  .uri("/some-resource")
  .retrieve()
  .onStatus(
    HttpStatus.INTERNAL_SERVER_ERROR::equals,
    response -> response.bodyToMono(String.class).map(Exception::new))

I'm trying to write some test cases for a function like this in a project that I'm working on. I can't figure out how do I access the response in the onStatus parameters, or how to pass a specific HTTP status code to this onStatus function to be able to simulate the response.

I don't really understand where the onStatus function is getting the HTTP status from, or where it is passing the response to. Am I missing something here?

  • post the actual code, not pictures – kerbermeister Feb 09 '23 at 06:37
  • @kerbermeister I've updated the post, sorry about that – hypernuggets Feb 09 '23 at 06:39
  • The `onStatus` method requires two parameters. The first is a predicate that takes in a status code. Execution of the second parameter is based on the output of the first. The second is a function that maps the response to a Mono or an Exception. That is to say, in your case, if the HTTP call to `/some-resource` returns HTTP status code `INTERNAL_SERVER_ERROR(500)`, an Exception will be returned instead of the response body. – Tonny Tc Feb 09 '23 at 11:27
  • Does this answer your question? [How to unit test the WebClient.onStatus() in java?](https://stackoverflow.com/questions/75114698/how-to-unit-test-the-webclient-onstatus-in-java) – Alex Feb 13 '23 at 17:25

0 Answers0