自定义全局异常处理器
@Component
public class GlobaExceptionResolve implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView mv = new ModelAndView();
if (e instanceof NullPointerException){
mv.setViewName("exception1");
}else if (e instanceof ArithmeticException){
mv.setViewName("exception2");
}
mv.addObject("msg",e.toString());
return mv;
}
}
单元测试junit
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {App.class})
public class UserServiceTest {
.....
}