use of org.springframework.web.util.NestedServletException in project spring-framework by spring-projects.
the class ExceptionHandlerExceptionResolverTests method resolveExceptionWithAssertionError.
@Test
void resolveExceptionWithAssertionError() throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class);
this.resolver.setApplicationContext(ctx);
this.resolver.afterPropertiesSet();
AssertionError err = new AssertionError("argh");
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, new NestedServletException("Handler dispatch failed", err));
assertThat(mav).as("Exception was not handled").isNotNull();
assertThat(mav.isEmpty()).isTrue();
assertThat(this.response.getContentAsString()).isEqualTo(err.toString());
}
use of org.springframework.web.util.NestedServletException in project neubbs by nuitcoder.
the class AccountControllerTest method testValidateCaptchaException.
/**
* 测试 /api/account/check-captcha
* - 检查用户输入验证码匹配异常
* - request param error
* - [✔] null
* - [✔] format not norm
* - service exception
* - [✔] not pass '/api/account/captcha' api to generate captcha picture, session not exist captcha text
*/
@Test
public void testValidateCaptchaException() throws Exception {
// build captcha
ICaptchaService captchaService = (ICaptchaService) webApplicationContext.getBean("captchaServiceImpl");
String captcha = captchaService.getCaptchaText();
// 1. request param error and 2. input captcha error
String[] params = { null, "123", "abc", "ABC", "123abcABC", "0*=123", "55555" };
for (String param : params) {
System.out.println("input captcha = " + param);
try {
mockMvc.perform(MockMvcRequestBuilders.get("/api/account/validate-captcha").sessionAttr(SetConst.SESSION_CAPTCHA, captcha).param(ParamConst.CAPTCHA, param)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value(ApiMessage.CAPTCHA_INCORRECT)).andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
} catch (NestedServletException ne) {
Assert.assertThat(ne.getRootCause(), CoreMatchers.anyOf(CoreMatchers.instanceOf(ParamsErrorException.class), CoreMatchers.instanceOf(ServiceException.class)));
}
}
// not pass '/api/account/validate' api to generate captcha picture, session not exist captcha text
try {
mockMvc.perform(MockMvcRequestBuilders.get("/api/account/validate-captcha").param(ParamConst.CAPTCHA, captcha)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(ApiMessage.CAPTCHA_INCORRECT)).andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
} catch (NestedServletException ne) {
Assert.assertSame(ServiceException.class, ne.getRootCause().getClass());
Assert.assertEquals(ApiMessage.NO_GENERATE_CAPTCHA, ne.getRootCause().getMessage());
}
util.printSuccessMessage();
}
Aggregations