### Question:
TrainController { @CrossOrigin(origins = "*") @GetMapping(value = "/trains") public HttpEntity query(@RequestHeader HttpHeaders headers) { List<TrainType> trainTypes = trainService.query(headers); if (trainTypes != null && !trainTypes.isEmpty()) { return ok(new Response(1, "success", trainTypes)); } else { return ok(new Response(0, "no content", trainTypes)); } }  @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 testQuery1() throws Exception { List<TrainType> trainTypes = new ArrayList<>(); trainTypes.add(new TrainType()); Mockito.when(trainService.query(Mockito.any(HttpHeaders.class))).thenReturn(trainTypes); String result = mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/trainservice/trains")) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); Assert.assertEquals("success", JSONObject.parseObject(result, Response.class).getMsg()); }
@Test public void testQuery2() throws Exception { List<TrainType> trainTypes = new ArrayList<>(); Mockito.when(trainService.query(Mockito.any(HttpHeaders.class))).thenReturn(trainTypes); String result = mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/trainservice/trains")) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); Assert.assertEquals("no content", JSONObject.parseObject(result, Response.class).getMsg()); }