0

In spring, I have found my MockMVC for a given url:

MvcResult result = this.mockMvc.perform( get(urlToFetch).sessionAttrs(sessionAttr))
            .andExpect(status().isOk())
            .andReturn();

Great. Now I want to see what the html that this would return is. How do I do that? I want a string representation of it (ie String theHtml = ...) so I can parse it and check it in my test.

bharal
  • 15,461
  • 36
  • 117
  • 195
  • Possible duplicate of [How to check String in response body with mockMvc](https://stackoverflow.com/questions/18336277/how-to-check-string-in-response-body-with-mockmvc) – Ori Marko May 27 '19 at 14:38

2 Answers2

0

You can simply use

String responseString = result.getResponse().getContentAsString();

Rishikesh Dhokare
  • 3,559
  • 23
  • 34
  • when I do that I get an empty string! But i know that this works (I'm adding the tests now, to already working code). Also, I can see that the mock is capturing the right data, and I can pull the model name etc from it. – bharal May 27 '19 at 14:53
  • It basically gets you whatever the response is in a string format, does not care if its html, json or something else. Are you sure there is no other component involved when you test it and when you actually hit the api manually? – Rishikesh Dhokare May 27 '19 at 14:58
  • how would I know what other component needs to be involved? – bharal May 27 '19 at 15:58
0

Oh.

There is no rendering of jsp content. So that's why I'm not getting anything.

bharal
  • 15,461
  • 36
  • 117
  • 195