Search in sources :

Example 21 with NestedServletException

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());
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) NestedServletException(org.springframework.web.util.NestedServletException) ModelAndView(org.springframework.web.servlet.ModelAndView) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 22 with NestedServletException

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();
}
Also used : ParamsErrorException(org.neusoft.neubbs.exception.ParamsErrorException) NestedServletException(org.springframework.web.util.NestedServletException) ServiceException(org.neusoft.neubbs.exception.ServiceException) ICaptchaService(org.neusoft.neubbs.service.ICaptchaService) Test(org.junit.Test)

Aggregations

NestedServletException (org.springframework.web.util.NestedServletException)22 IOException (java.io.IOException)5 Test (org.junit.Test)5 RemoteInvocation (org.springframework.remoting.support.RemoteInvocation)4 RemoteInvocationResult (org.springframework.remoting.support.RemoteInvocationResult)4 ServletException (javax.servlet.ServletException)3 Test (org.junit.jupiter.api.Test)3 WebAsyncManager (org.springframework.web.context.request.async.WebAsyncManager)3 ServletException (jakarta.servlet.ServletException)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Map (java.util.Map)2 Transactional (javax.transaction.Transactional)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 SerializationException (com.haulmont.cuba.core.sys.serialization.SerializationException)1 User (com.serotonin.m2m2.vo.User)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 RequestDispatcher (jakarta.servlet.RequestDispatcher)1