Search in sources :

Example 31 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleMissingPathVariable.

@Test
public void handleMissingPathVariable() throws NoSuchMethodException {
    Method method = getClass().getMethod("handle", String.class);
    MethodParameter parameter = new MethodParameter(method, 0);
    MissingPathVariableException ex = new MissingPathVariableException("foo", parameter);
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertNotNull("No ModelAndView returned", mav);
    assertTrue("No Empty ModelAndView returned", mav.isEmpty());
    assertEquals("Invalid status code", 500, response.getStatus());
    assertEquals("Missing URI template variable 'foo' for method parameter of type String", response.getErrorMessage());
}
Also used : MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) ModelAndView(org.springframework.web.servlet.ModelAndView) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 32 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleMethodArgumentNotValid.

@Test
public void handleMethodArgumentNotValid() throws Exception {
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean");
    errors.rejectValue("name", "invalid");
    MethodParameter parameter = new MethodParameter(this.getClass().getMethod("handle", String.class), 0);
    MethodArgumentNotValidException ex = new MethodArgumentNotValidException(parameter, errors);
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertNotNull("No ModelAndView returned", mav);
    assertTrue("No Empty ModelAndView returned", mav.isEmpty());
    assertEquals("Invalid status code", 400, response.getStatus());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.tests.sample.beans.TestBean) ModelAndView(org.springframework.web.servlet.ModelAndView) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) Test(org.junit.Test)

Example 33 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ServletResponseMethodArgumentResolverTests method servletResponseNoMavContainer.

// SPR-8983
@Test
public void servletResponseNoMavContainer() throws Exception {
    MethodParameter servletResponseParameter = new MethodParameter(method, 0);
    assertTrue("ServletResponse not supported", resolver.supportsParameter(servletResponseParameter));
    Object result = resolver.resolveArgument(servletResponseParameter, null, webRequest, null);
    assertSame("Invalid result", servletResponse, result);
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 34 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ServletResponseMethodArgumentResolverTests method writer.

@Test
public void writer() throws Exception {
    MethodParameter writerParameter = new MethodParameter(method, 2);
    assertTrue("Writer not supported", resolver.supportsParameter(writerParameter));
    Object result = resolver.resolveArgument(writerParameter, mavContainer, webRequest, null);
    assertSame("Invalid result", servletResponse.getWriter(), result);
    assertTrue(mavContainer.isRequestHandled());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 35 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ServletResponseMethodArgumentResolverTests method servletResponse.

@Test
public void servletResponse() throws Exception {
    MethodParameter servletResponseParameter = new MethodParameter(method, 0);
    assertTrue("ServletResponse not supported", resolver.supportsParameter(servletResponseParameter));
    Object result = resolver.resolveArgument(servletResponseParameter, mavContainer, webRequest, null);
    assertSame("Invalid result", servletResponse, result);
    assertTrue(mavContainer.isRequestHandled());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)322 Test (org.junit.Test)251 Method (java.lang.reflect.Method)65 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 HandlerMethod (org.springframework.web.method.HandlerMethod)28 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19