mardi 22 septembre 2020

How to test custom ExceptionHandlers for annotations referenced by @ControllerAdvice?

I am working on a service broker based on the Spring Cloud Open Service Broker framework. I'm using a custom ExceptionHandler which inherits from ServiceBrokerExceptionHandler to adjust the HTTP status code for certain situations on all the ServiceBrokerRestControllers:

@ControllerAdvice(annotations = ServiceBrokerRestController.class)
public class MyServiceBrokerExceptionHandler extends ServiceBrokerExceptionHandler {

    @ExceptionHandler({MyCustomException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ErrorMessage handleException(MyCustomException ex) {
        return this.getErrorResponse(ex);
    }

}

Now I want to test it but I don't really understand how. Since the handler is applied to all controllers annotated with @ServiceBrokerRestController I don't really care about a certain controller bean let aside a certain method. I just want to "mock" some kind of ServiceBrokerRestController and given one of its methods throws a MyCustomException the web layer should return with 500 - Internal Server Error. Do I have to create a ServiceBrokerRestController dummy for that or is there a better way?

Aucun commentaire:

Enregistrer un commentaire