I am working with Spring Framework 4.3.13.RELEASE
About Rest, for POST
, I have the following:
@Controller @RequestMapping(path="/personas") public class PersonaRestController { @PostMapping(consumes={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE}) public ResponseEntity<Void> saveOne( @Validated @RequestBody Persona persona, ...
About Testing working with JUnit
and with ResultActions
I can get the java.lang.AssertionError: No handler
error message and confirm the following:
MockHttpServletRequest: HTTP Method = POST Request URI = /personas Parameters = {} Headers = {} Handler: Type = null Async: Async started = false Async result = null Resolved Exception: Type = org.springframework.web.HttpMediaTypeNotSupportedException ModelAndView: View name = null View = null Model = null FlashMap: Attributes = null MockHttpServletResponse: Status = 415 Error message = null Headers = {Accept=[application/xml, application/json;charset=UTF-8]} Content type = null Body = Forwarded URL = null Redirected URL = null Cookies = []
Has sense this behaviour because the saveOne
method expects the content data be either XML
or JSON
and for this test scenario the Content-Type
was not sent.
Therefore is expected that Spring arises a HttpMediaTypeNotSupportedException
instance. Then the @Test
method fails and the java.lang.AssertionError: No handler
error message appears because there is no a handler to handle the user request.
Until here all OK.
Again, about Rest, for GET
, I have the following:
@GetMapping(path="/{id}", produces={MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE}) public @ResponseBody Persona findOneById(@PathVariable String id){ return personaService.findOneById(id); }
For other Testing scenario with the following:
MockHttpServletRequest: HTTP Method = GET Request URI = /personas/100 Parameters = {} Headers = {} Handler: Type = com.manuel.jordan.rest.persona.PersonaRestController Method = public com.manuel.jordan.domain.Persona com.manuel.jordan.rest.persona.PersonaRestController.findOneById(java.lang.String)
My interceptor shows the following data
HttpHeaderControlInterceptor - preHandle HttpServletRequest > URL: http://localhost/personas/100 URI: /personas/100 HttpMethod: GET Accept: null Accept-Language: null Content-Type: null
I am confused why the findOneById
method was called how a handler even when Accept
was null. I had expected something similar than the java.lang.AssertionError: No handler
error message and the HttpMediaTypeNotAcceptableException class instead, it because the findOneById
method can return the data in either XML
or JSON
What is missing?