1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| @RunWith(SpringRunner.class) @WebMvcTest public class CarsControllerTests {
@Autowired private MockMvc mockMvc;
@MockBean private CarService carService;
@Test public void getCar_WithName_ReturnsCar() throws Exception { when(this.carService.getCarDetails("prius")).thenReturn(new Car("prius", "hybrid")); this.mockMvc.perform(get("/cars/{name}", "prius")) .andExpect(status().isOk()) .andExpect(jsonPath("name").value("prius")) .andExpect(jsonPath("type").value("hybrid")); }
@Test public void getCar_NotFound_Returns404() throws Exception { when(this.carService.getCarDetails(any())).thenReturn(null); this.mockMvc.perform(get("/cars/{name}", "prius")) .andExpect(status().isNotFound()); }
}
|