Instantly share code, notes, and snippets.

Embed
What would you like to do?
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