After using the answer from @eugene-to and another similar one here I found limitations and raised an issue on Spring: https://jira.spring.io/browse/SPR-12751
As a result, Spring test introduced the ability to register @ControllerAdvice
classes in the builder in 4.2. If you are using Spring Boot then you will need 1.3.0 or later.
With this improvement, if you are using standalone setup then you can set one or more ControllerAdvice
instances like so:
mockMvc = MockMvcBuilders.standaloneSetup(yourController)
.setControllerAdvice(new YourControllerAdvice())
.build();
Note: the name setControllerAdvice()
may not make it immediately obvious but you can pass many instances to it, since it has a var-args signature.