1

I have a servlet defined in web.xml, so I defined it inside a Controller for testing only for MyResource:

 @Controller 
 public class TestMyServlet {

     MyResource servlet;

     @Autowired
     AutowireCapableBeanFactory beanFac;

     @PostConstruct
     void init() {
         servlet = new MyResource();
         beanFac.autowireBean(servlet);
     }

     @RequestMapping(value = "/servlet/api/update", method = RequestMethod.POST)
     public MyResponse handle(HttpServletRequest request, HttpServletResponse response, @RequestBody String json) {     
         return servlet.update(json);
     }

Then I test it using MockHttpServletRequestBuilder:

  MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders
    .post("/servlet/api/update")
    .content("{\"id\":1,\"toggle\":true}]")
    .session(httpSession)
    .contentType(MediaType.APPLICATION_JSON);
    MvcResult mvcResult = this.mockMvc.perform(mockHttpServletRequestBuilder)
              .andDo(MockMvcResultHandlers.print())
              .andReturn();
    ModelAndView modelAndView = mvcResult.getModelAndView().getModelMap();
    String response = mvcResult.getResponse().getContentAsString();

In servlet I don't use ModelAndView, just returning a POJO object (MyResponse) that is serialize to JSON response

I'm seeing MyResponse object as a second attribute in ModelAndView, but response is null

How can I check JSON string returned in this response?

4
  • Debug the code. See what's happening. Log the response or add logging and see where is it null Commented Feb 25, 2019 at 13:59
  • @SagarKharab in my code I'm only returning an object which is fine, the question is about how to test it using Spring MvcResult Commented Feb 25, 2019 at 14:01
  • maybe completely off topic, but is there a typo in @RequestMapping(value = "/servletapi/update" ? Commented Feb 25, 2019 at 14:30
  • @Daniele only typo, my endpoint working and returning POJO successfully Commented Feb 25, 2019 at 14:31

1 Answer 1

2

There is one way here, i hope i understood the question correct ,

MockMvcRequestBuilders
.post("/servlet/api/update")
.content("{\"id\":1,\"toggle\":true}]")
.session(httpSession)
.contentType(MediaType.APPLICATION_JSON)
.andExpect(jsonPath("$[0].key",is("value")));

andExpect(jsonPath("$[0].key",is("value")))

you can use this for each key and value.

Or

try this ,

 MockMvcRequestBuilders
.post("/servlet/api/update")
.content("{\"id\":1,\"toggle\":true}]")
.session(httpSession)
.contentType(MediaType.APPLICATION_JSON)
.andExpect(content().string(containsString("value")));
Sign up to request clarification or add additional context in comments.

5 Comments

method is(String) is undefined, what is the static import?
import static org.hamcrest.Matchers.is;
we don't use hamcrest yet
Sorry but also method containsString(String) is undefined
i am so sorry i didn't realize its again from hamcrest, but would it not solve your problem if you use it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.