When REST controller returns JSON here is the method to test it
JSON String
[{"id":1,"name":"Sam"},{"id":2,"name":"David"}]
Test
MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void shouldGiveSingleDataSetForAvailabilityTypeService() {
try {
mockMvc.perform(get("/person/1").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("name", is("Sam")));
mockMvc.perform(get("/person/2").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("name", is("David")));
} catch (Exception e) {
log.error(e.getMessage(), e);
fail();
}
JSON String
[{"id":1,"name":"Sam"},{"id":2,"name":"David"}]
Test
MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void shouldGiveSingleDataSetForAvailabilityTypeService() {
try {
mockMvc.perform(get("/person/1").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("name", is("Sam")));
mockMvc.perform(get("/person/2").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(jsonPath("name", is("David")));
} catch (Exception e) {
log.error(e.getMessage(), e);
fail();
}