spring 3.2 testing the requestmappings are there and working, but the java is not invoked

StackOverflow https://stackoverflow.com/questions/17882817

  •  04-06-2022
  •  | 
  •  

سؤال

All my mappings are working correctly :

 MvcResult mvcResult = this.mockMvc.perform(get("/company/doSomething"))
                    .andDo(print())
                    .andExpect(status().isOk())
                    .andReturn();

returns status ok from this

@RequestMapping(value = "/company/doSomething", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Boolean myMethod() {
      return false;
    }

if I set a breakpoint in the controller method, and run test n debug mode the test just passes (the breakpoint is not invoked) and prints out the responses - nothing is returned and I get this :

MockHttpServletResponse:
              Status = 200
       Error message = null
             Headers = {}
        Content type = null
                Body = 
       Forwarded URL = default
      Redirected URL = null
             Cookies = []

and the actual request looks like this

MockHttpServletRequest:
         HTTP Method = GET
         Request URI = /company/doSomething
          Parameters = {}
             Headers = {}

             Handler:
                Type = org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
هل كانت مفيدة؟

المحلول

From the actual handler selected (i.e. DefaultServletHttpRequestHandler) I can see that no annotated controller method matched. Somehow the request as specified does not match the controller method request mapping.

نصائح أخرى

Do you have the @Controller annotation on your class?

Also do you have the Jackson2 (JSON) libraries in your classpath?

When I copy your code and run the tests, I get the correct response output:

MockHttpServletResponse:
          Status = 200
   Error message = null
         Headers = {Content-Type=[application/json]}
    Content type = application/json
            Body = false
   Forwarded URL = null
  Redirected URL = null
         Cookies = []

However my request output states the correct handler

MockHttpServletRequest:
     HTTP Method = GET
     Request URI = /company/doSomething
      Parameters = {}
         Headers = {}
         Handler:
            Type = example.TestController
          Method = public java.lang.Boolean example.TestController.myMethod()

whereas your output states that the default DefaultServletHttpRequestHandler handler is handling the request.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top