Write JUnit test for @ExceptionHandler

后端 未结 3 2284
-上瘾入骨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:12

    You could change @Test to

    @Test(expected=NotAuthorizedException.class)
    

    This would return true if the internals throw up that exception and false otherwise.

    This would also make the assertThat() unnecessary. You could write a second test that catches the NotAuthorizedException then you could inspect the responseMock under that condition then.

提交回复
热议问题