5

I'm trying to unit test my controller and the specific case which is : my service return a Mono.Empty, I throw a NotFoundException and I wan't to make sure I'm getting a 404 exception

here's my controller :

@GetMapping(path = "/{id}")
    public Mono<MyObject<JsonNode>> getFragmentById(@PathVariable(value = "id") String id) throws NotFoundException {

        return this.myService.getObject(id, JsonNode.class).switchIfEmpty(Mono.error(new NotFoundException()));

    }

Here's my controller advice :

@ControllerAdvice
public class RestResponseEntityExceptionHandler {

    @ExceptionHandler(value = { NotFoundException.class })
    protected ResponseEntity<String> handleNotFound(SaveActionException ex, WebRequest request) {
        String bodyOfResponse = "This should be application specific";
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Resource not found");
    }

}

and my test :

@Before
    public void setup() {
        client = WebTestClient.bindToController(new MyController()).controllerAdvice(new RestResponseEntityExceptionHandler()).build();
    }
@Test
    public void assert_404() throws Exception {

        when(myService.getobject("id", JsonNode.class)).thenReturn(Mono.empty());

        WebTestClient.ResponseSpec response = client.get().uri("/api/object/id").exchange();
        response.expectStatus().isEqualTo(404);

    }

I'm getting a NotFoundException But a 500 error not a 404 which mean my advice hasn't been called

stack trace :

java.lang.AssertionError: Status expected:<404> but was:<500>

> GET /api/fragments/idFragment
> WebTestClient-Request-Id: [1]

No content

< 500 Internal Server Error
< Content-Type: [application/json;charset=UTF-8]

Content not available yet

any idea ?

Seb
  • 3,602
  • 8
  • 36
  • 52

1 Answers1

2

I believe you can delete this controller advice and just have the following:

    @GetMapping(path = "/{id}")
    public Mono<MyObject<JsonNode>> getFragmentById(@PathVariable(value = "id") String id) {

        return this.myService.getObject(id, JsonNode.class)
                             .switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)));

    }

As for ResponseEntityExceptionHandler, this class is part of Spring MVC so I don't think you should use it in a WebFlux application.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
  • Hi, thanks for your reply. actually I found example of ControllerAdvice used with webflux so I think I shoudl be able to use it – Seb Jul 03 '17 at 13:11
  • Fair enough. Having `ResponseEntityExceptionHandler` means you probably have spring-webmvc on the classpath (you shouldn't). Can you try removing that dependency from your project and not extending from `ResponseEntityExceptionHandler`? – Brian Clozel Jul 03 '17 at 13:29
  • Is there a stacktrace you can share? – Brian Clozel Jul 03 '17 at 13:55
  • not much in the stack trace, still I added it to the post – Seb Jul 03 '17 at 14:52
  • I meant a stacktrace of the server application (if there's one). Does this work when running the app and sending a curl request (i.e. not using the test infrastructure)? – Brian Clozel Jul 03 '17 at 14:57