Skip to content

Instantly share code, notes, and snippets.

@Jet-C
Created December 1, 2019 01:50
Show Gist options
  • Save Jet-C/7568ca6ddf669e7cc388f22dd1ddd0db to your computer and use it in GitHub Desktop.
Save Jet-C/7568ca6ddf669e7cc388f22dd1ddd0db to your computer and use it in GitHub Desktop.
Controller layer testing for vehicle service
@WebMvcTest(VehicleController.class)
@ActiveProfiles("test")
public class DemoWebLayerTest {
/*
* We can @Autowire MockMvc because the WebApplicationContext provides an
* instance/bean for us
*/
@Autowired
MockMvc mockMvc;
/*
* Jackson mapper for Object -> JSON conversion
*/
@Autowired
ObjectMapper mapper;
/*
* We use @MockBean because the WebApplicationContext does not provide
* any @Component, @Service or @Repository beans instance/bean of this service
* in its context. It only loads the beans solely required for testing the
* controller.
*/
@MockBean
VehicleService vechicleService;
@Test
public void get_allVehicles_returnsOkWithListOfVehicles() throws Exception {
List<Vehicle> vehicleList = new ArrayList<>();
Vehicle vehicle1 = new Vehicle("AD23E5R98EFT3SL00", "Ford", "Fiesta", 2016, false);
Vehicle vehicle2 = new Vehicle("O90DEPADE564W4W83", "Volkswagen", "Jetta", 2016, false);
vehicleList.add(vehicle1);
vehicleList.add(vehicle2);
// Mocking out the vehicle service
Mockito.when(vechicleService.getAllVehicles()).thenReturn(vehicleList);
mockMvc.perform(MockMvcRequestBuilders.get("/demo/vehicles").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].vin", is("AD23E5R98EFT3SL00"))).andExpect(jsonPath("$[0].make", is("Ford")))
.andExpect(jsonPath("$[1].vin", is("O90DEPADE564W4W83")))
.andExpect(jsonPath("$[1].make", is("Volkswagen")));
}
@Test
public void post_createsNewVehicle_andReturnsObjWith201() throws Exception {
Vehicle vehicle = new Vehicle("AD23E5R98EFT3SL00", "Ford", "Fiesta", 2016, false);
Mockito.when(vechicleService.createVehicle(Mockito.any(Vehicle.class))).thenReturn(vehicle);
// Build post request with vehicle object payload
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/demo/create/vehicle")
.contentType(MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON)
.characterEncoding("UTF-8").content(this.mapper.writeValueAsBytes(vehicle));
mockMvc.perform(builder).andExpect(status().isCreated()).andExpect(jsonPath("$.vin", is("AD23E5R98EFT3SL00")))
.andExpect(MockMvcResultMatchers.content().string(this.mapper.writeValueAsString(vehicle)));
}
@Test
public void post_submitsInvalidVehicle_WithEmptyMake_Returns400() throws Exception {
// Create new vehicle with empty 'make' field
Vehicle vehicle = new Vehicle("AD23E5R98EFT3SL00", "", "Firebird", 1982, false);
String vehicleJsonString = this.mapper.writeValueAsString(vehicle);
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.post("/demo/create/vehicle/")
.contentType(MediaType.APPLICATION_JSON).content(vehicleJsonString)).andExpect(status().isBadRequest());
// @Valid annotation in controller will cause exception to be thrown
assertEquals(MethodArgumentNotValidException.class,
resultActions.andReturn().getResolvedException().getClass());
assertTrue(resultActions.andReturn().getResolvedException().getMessage().contains("'make' field was empty"));
}
@Test
public void put_updatesAndReturnsUpdatedObjWith202() throws Exception {
Vehicle vehicle = new Vehicle("AD23E5R98EFT3SL00", "Ford", "Fiesta", 2016, false);
Mockito.when(vechicleService.updateVehicle("AD23E5R98EFT3SL00", vehicle)).thenReturn(vehicle);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
.put("/demo/update/vehicle/AD23E5R98EFT3SL00", vehicle).contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON).characterEncoding("UTF-8")
.content(this.mapper.writeValueAsBytes(vehicle));
mockMvc.perform(builder).andExpect(status().isAccepted()).andExpect(jsonPath("$.vin", is("AD23E5R98EFT3SL00")))
.andExpect(MockMvcResultMatchers.content().string(this.mapper.writeValueAsString(vehicle)));
}
@Test
public void delete_deleteVehicle_Returns204Status() throws Exception {
String vehicleVin = "AD23E5R98EFT3SL00";
VehicleService serviceSpy = Mockito.spy(vechicleService);
Mockito.doNothing().when(serviceSpy).deleteVehicle(vehicleVin);
mockMvc.perform(MockMvcRequestBuilders.delete("/demo/vehicles/AD23E5R98EFT3SL00")
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
verify(vechicleService, times(1)).deleteVehicle(vehicleVin);
}
@Test
public void get_vehicleByVin_ThrowsVehicleNotFoundException() throws Exception {
// Return an empty Optional object since we didn't find the vin
Mockito.when(vechicleService.getVehicleByVin("AD23E5R98EFT3SL00")).thenReturn(Optional.empty());
ResultActions resultActions = mockMvc.perform(
MockMvcRequestBuilders.get("/demo/vehicles/AD23E5R98EFT3SL00").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
assertEquals(VehicleNotFoundException.class, resultActions.andReturn().getResolvedException().getClass());
assertTrue(resultActions.andReturn().getResolvedException().getMessage()
.contains("Vehicle with VIN (" + "AD23E5R98EFT3SL00" + ") not found!"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment