### Question:
TrainController { @CrossOrigin(origins = "*") @PutMapping(value = "/trains") public HttpEntity update(@RequestBody TrainType trainType, @RequestHeader HttpHeaders headers) { boolean isUpdateSuccess = trainService.update(trainType, headers); if (isUpdateSuccess) { return ok(new Response(1, "update success", isUpdateSuccess)); } else { return ok(new Response(0, "there is no trainType with the trainType id", isUpdateSuccess)); } }  @GetMapping(path = "/trains/welcome") String home(@RequestHeader HttpHeaders headers); @CrossOrigin(origins = "*") @PostMapping(value = "/trains") HttpEntity create(@RequestBody TrainType trainType, @RequestHeader HttpHeaders headers); @CrossOrigin(origins = "*") @GetMapping(value = "/trains/{id}") HttpEntity retrieve(@PathVariable String id, @RequestHeader HttpHeaders headers); @CrossOrigin(origins = "*") @PutMapping(value = "/trains") HttpEntity update(@RequestBody TrainType trainType, @RequestHeader HttpHeaders headers); @CrossOrigin(origins = "*") @DeleteMapping(value = "/trains/{id}") HttpEntity delete(@PathVariable String id, @RequestHeader HttpHeaders headers); @CrossOrigin(origins = "*") @GetMapping(value = "/trains") HttpEntity query(@RequestHeader HttpHeaders headers);  }### Answer:
@Test public void testUpdate1() throws Exception { TrainType trainType = new TrainType(); Mockito.when(trainService.update(Mockito.any(TrainType.class), Mockito.any(HttpHeaders.class))).thenReturn(true); String requestJson = JSONObject.toJSONString(trainType); String result = mockMvc.perform(MockMvcRequestBuilders.put("/api/v1/trainservice/trains").contentType(MediaType.APPLICATION_JSON).content(requestJson)) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); Assert.assertEquals(new Response(1, "update success", true), JSONObject.parseObject(result, Response.class)); }
@Test public void testUpdate2() throws Exception { TrainType trainType = new TrainType(); Mockito.when(trainService.update(Mockito.any(TrainType.class), Mockito.any(HttpHeaders.class))).thenReturn(false); String requestJson = JSONObject.toJSONString(trainType); String result = mockMvc.perform(MockMvcRequestBuilders.put("/api/v1/trainservice/trains").contentType(MediaType.APPLICATION_JSON).content(requestJson)) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); Assert.assertEquals(new Response(0, "there is no trainType with the trainType id", false), JSONObject.parseObject(result, Response.class)); }