Testing Spring MVC @ExceptionHandler method with Spring MVC Test

前端 未结 7 1825
悲&欢浪女
悲&欢浪女 2020-12-04 20:56

I have the following simple controller to catch any unexpected exceptions:

@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(Throwa         


        
7条回答
  •  庸人自扰
    2020-12-04 21:28

    I had the same issue; This worked for me:

    *RuntimeException in thenThrow(); ErrorHandler won't catch Exception or Throwable parent classes.
    *Remove all try-catch sentences of methods being tested
    *No @Before or @ComponentScan annotations were needed at all

    when(getRejectionQuery.execute(anyInt(), anyInt(), anyMap())).thenThrow(new RuntimeException());
    

提交回复
热议问题