Mock MVC @ControllerAdvice exception handlers

2,960 views
Skip to first unread message

nat...@ncjones.com

unread,
Aug 5, 2014, 8:32:08 AM8/5/14
to rest-a...@googlegroups.com
I have Spring exception handlers that are common to all of my request mappings defined in a class annotated with @ControllerAdvice. Is it possible to make Rest Assured MockMVC aware of those exception handlers so that error responses work as expected during test execution?

Example controller advice:

@ControllerAdvice
public class GlobalExceptionController {

   
@ExceptionHandler(AccessDeniedException.class)
   
@ResponseStatus(HttpStatus.UNAUTHORIZED)
   
public @ResponseBody
   
String handleAllRuntimeException(AccessDeniedException ex) {
       
return ex.getMessage();
   
}

}


Johan Haleby

unread,
Aug 5, 2014, 11:50:24 AM8/5/14
to rest-a...@googlegroups.com
It may work if you bootstrap RA with a webAppContextSetup instead of standalone setup. Not sure though.. Otherwise you may need to bootstrap the test as a full-blown integration test (@IntegrationTest if you're using Spring Boot) and test it using vanilla REST Assured. I've been forced to use this approach several times when I want to test filters etc (and not have to define them in my test which I don't believe is a good idea).

/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nathan Jones

unread,
Aug 17, 2014, 12:59:43 AM8/17/14
to rest-a...@googlegroups.com
I got this working with webAppContextSetup by following the tutorial at
http://www.petrikainulainen.net/spring-mvc-test-tutorial/. Although I
ended up switching to Spring Mock MVC I'm pretty sure the same approach
would have worked for Rest Assured.

- Nathan
> <mailto:rest-assured...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "REST assured" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to rest-assured...@googlegroups.com
> <mailto:rest-assured...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages