Write JUnit test for @ExceptionHandler

后端 未结 3 2309
-上瘾入骨i
-上瘾入骨i 2021-02-19 21:35

I am writing a Rest service using Spring MVC. Here is the outline of the class:

 @Controller
 public class MyController{

     @RequestMapping(..)
     public vo         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-19 22:10

    Annotate your Exception Handling controller with @ControllerAdvice instead of @Controller.

    As Boris Treukhov noted when adding the @ExceptionHandler annotation to a method in the controller that throws the exception will make it work but only from that specific controller.

    @ControllerAdvice will allow your exception handeling methods to be applicable for your whole application not just one specific controller.

提交回复
热议问题