单元测试之异常捕获

两种方式

  1. 测试 该接口的单元测试的方法,在@Test 里面加上异常的 class名称
@Test(expectedExceptions= {NestedServletException.class})
public void testCreateServiceWithInvalidParamters() throws Exception {
  .....
}
  1. 使用 try catch 接收
@Test
public void testCreateServiceWithInvalidParamters() throws Exception {
  .....
   try {
            mvc.perform(request).andReturn();
        } catch (NestedServletException e) {
            assertTrue(e.getCause().getMessage().endsWith(ROLLBACK_SERVICE_REQUEST_PARAM_EMPTY));
        }
}
posted @ 2021-03-02 08:39  zendwang  阅读(914)  评论(0编辑  收藏  举报