Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dhaval201279
Last active March 11, 2018 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhaval201279/00b2372a00db048562eadbcec4b571ff to your computer and use it in GitHub Desktop.
Save dhaval201279/00b2372a00db048562eadbcec4b571ff to your computer and use it in GitHub Desktop.
3rd version of ReservationControllerTest
@RunWith(SpringRunner.class)
@WebMvcTest(ReservationController.class)
public class ReservationControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
ReservationService reservationService;
@Test
public void getReservation_shouldReturnReservationInfo() {
BDDMockito.given(reservationService.getReservationDetails(ArgumentMatchers.anyString()))
.willReturn(new Reservation(Long.valueOf(1), "Dhaval", "Shah"));
try {
mockMvc.perform(MockMvcRequestBuilders.get("/reservation/Dhaval"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("firstName").value("Dhaval"))
.andExpect(MockMvcResultMatchers.jsonPath("lastName").value("Shah"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getReservation_NotFound() throws Exception {
BDDMockito.given(reservationService.getReservationDetails(ArgumentMatchers.anyString()))
.willThrow(new ReservationNotFoundException());
mockMvc.perform(MockMvcRequestBuilders.get("/reservation/Dhaval"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment