### Question:
TrainController { @CrossOrigin(origins = "*") @GetMapping(value = "/trains/{id}") public HttpEntity retrieve(@PathVariable String id, @RequestHeader HttpHeaders headers) { TrainType trainType = trainService.retrieve(id, headers); if (trainType == null) { return ok(new Response(0, "here is no TrainType with the trainType id: " + id, null)); } else { return ok(new Response(1, "success", trainType)); } }  @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 testRetrieve1() throws Exception { Mockito.when(trainService.retrieve(Mockito.anyString(), Mockito.any(HttpHeaders.class))).thenReturn(null); String result = mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/trainservice/trains/wrong_id")) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); Assert.assertEquals(new Response(0, "here is no TrainType with the trainType id: wrong_id", null), JSONObject.parseObject(result, Response.class)); }
@Test public void testRetrieve2() throws Exception { TrainType trainType = new TrainType(); Mockito.when(trainService.retrieve(Mockito.anyString(), Mockito.any(HttpHeaders.class))).thenReturn(trainType); String result = mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/trainservice/trains/id")) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); Assert.assertEquals("success", JSONObject.parseObject(result, Response.class).getMsg()); }