博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SpringBoot - 10异常处理与单元测试

Posted on 2021-01-03 15:52  Kingdomer  阅读(146)  评论(0编辑  收藏  举报

SpringBoot - 10异常处理与单元测试

(1) 异常处理

SpringBoot中对于异常处理提供了五种处理方式

(1.1)自定义错误页面

SpringBoot默认的处理异常的机制:SpringBoot默认的已经提供了一套处理异常的机制。
  • 一旦程序中出现了异常SpringBoot会向/error的url发送请求。
    • 在SpringBoot中提供了一个名为BasicErrorController来处理/error请求,然后跳转到默认显示异常的页面来展示异常信息。
  • 如果我们将所有的异常统一跳转到自定义的错误页面,需要在src/main/resources/templates目录下创建error.html页面。
    • 注意:页面名称必须为error。

templates/error.html

<html>
<head>
    <title>错误页</title>
</head>
<body>
    出错了,请与管理员联系。
</body>
</html>

 

(1.2)通过@ExceptionHandler注解处理异常

templates/error1.html

<body>
    出错了,请与管理员联系。
    <span th:text="${err}"/>
</body>
    @ExceptionHandler(value = {java.lang.NullPointerException.class})
    public ModelAndView nullPointExceptionHandler(Exception e){
        ModelAndView mv = new ModelAndView();
        mv.addObject("err",e.toString());
        mv.setViewName("error1");
        return mv;
    }

 (1.3)通过@ControllerAdvice与@ExceptionHandler注解处理异常

@ControllerAdvice  // 全局异常处理类
public class GlobalException {

    @ExceptionHandler(value = {java.lang.NullPointerException.class})
    public ModelAndView nullPointExceptionHandler(Exception e){
        ModelAndView mv = new ModelAndView();
        mv.addObject("err",e.toString());
        mv.setViewName("error1");
        return mv;
    }
    @ExceptionHandler(value = {java.lang.ArithmeticException.class})
    public ModelAndView arithmeticExceptionHandler(Exception e){
        ModelAndView mv = new ModelAndView();
        mv.addObject("err",e.toString());
        mv.setViewName("error2");
        return mv;
    }

}

templates/error2.html

<body>
    出错了,请与管理员联系。<br/>
    数据计算出错:<span th:text="${err}"/>
</body>

(1.4)通过SimpleMappingExceptionResolver对象处理异常

@Configuration
public class GlobalException2 {

    @Bean
    public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        Properties properties = new Properties();
        properties.put("java.lang.NullPointerException","error3"); // 参数一: 异常类型,全名; 参数二: 视图名称
        properties.put("java.lang.ArithmeticException","error4");

        resolver.setExceptionMappings(properties);
        return resolver;
    }
}

(1.5)通过自定义HandlerExceptionResolver对象处理异常

@Configuration
public class GlobalException3 implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView mv = new ModelAndView();
        if(e instanceof NullPointerException){
            mv.setViewName("error5");
        }
        if(e instanceof ArithmeticException){
            mv.setViewName("error6");
        }
        mv.addObject("error",e.toString());
        return mv;
    }
}

 

(2)单元测试

SpringBoot2.x使用 Junit5作为测试平台

(2.1)修改POM文件,引入GAV坐标

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <!--junit-vintage-engine 提供了Junit3与Junit4的运行平台-->
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

(2.2)测试

@SpringBootTest(classes = SpringBoot11JunitApplication.class)
class SpringBoot11JunitApplicationTests {

    @Autowired
    private UserService userService;

    @Test
    public void testAdd(){
        System.out.println(userService);
        this.userService.addUser();
    }
    
    @Test
    public void testStringSplit(){
        String str= "ni:hao:123";
        int index = str.indexOf(":");
        System.out.println(str.substring(index+1));

    }
}