How to set servlet path for every request through MockMvc

后端 未结 1 1488
梦谈多话
梦谈多话 2021-01-05 01:23

Is it possible to set the servlet path for all requests (get, post, put, delete) which go through the MockMvc?

The Spring dispatch servlet is mapped to /rest/* But i

相关标签:
1条回答
  • 2021-01-05 01:49

    I've had problems with ServletException("Circular view path ...") which happened only in real deployment but never in our tests with MockMvc.

    The problem was that a method was not annotated with @ResponseBody. The test worked fine as there was an empty servlet path so it resolved a viewName to 'servletPath/callPath' which was different from 'callPath' so it did not throw the ServletException. Hence I needed to set servletPath on test requests to get closer to how the app is deployed and get our tests to fail in case one forgets the annotation.

    .defaultRequest(get("/").servletPath("/main")) 
    

    worked for me like a charm. So the answer in the question works.

    0 讨论(0)
提交回复
热议问题