0

I have a method and method includes one exception if some param does not write in url. Let's show below

if (url != null) {
File = getService().getByUrl(url);
} else {
throw new IllegalStateException("you must send id);
}

this is my method pair and I want to write test for exception. How to I cath error message with mockmvc?

return getMockMvc().perform(get(url))
.contentType(MediaType.APPLICATION_JSON_UTF8)
.andExpect(status().is5xxServerError());

This code not getting with error message. Can you help me

sticky bit
  • 36,626
  • 12
  • 31
  • 42
selo
  • 61
  • 2
  • 7

1 Answers1

0

You can do something like:

return getMockMvc().perform(get(url)) .contentType(MediaType.APPLICATION_JSON_UTF8) .andExpect(status().is5xxServerError())
andExpect(content().string(containsString("you must send id")));
  • `try{ getMockMvc().perform(get(url)) .contentType(MediaType.APPLICATION_JSON_UTF8) .andExpect(status().is5xxServerError()) .andExpect(content().string("you must send id")); } catch (Exception e) { e.printStackTrace(); }` I solved problem use this small code – selo Apr 04 '19 at 12:01
  • Hi, Selo. Thanks for the feedback. If the answer help you, please mark this answer as "correct answer". – Allan Moreira Leite Apr 04 '19 at 16:18