I'm learning to write integration tests for a SpringBoot REST api, and when I send garbage data, the tests still pass. I would expect the ".andExpect(status().isCreated())" to throw an exception and fail the test, but it isn't?
@SpringBootTest
@AutoConfigureMockMvc
class BorrowerApplicationIntegrationTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private BookLoanDAO bookLoanDao;
@Autowired
private BorrowerController controller;
@Test
@Transactional
void checkoutBook() throws Exception {
BookLoanId bookLoanId = new BookLoanId(2, 2, 2); // This book does not exist in this branch
mockMvc.perform(post("/borrowers/book/checkout")
.contentType("application/json")
.content(objectMapper.writeValueAsString(bookLoanId)))
.andExpect(status().isCreated()); // The returned response is a 404 yet the test passes
List<BookLoan> bookLoans = bookLoanDao.findAll().stream()
.filter(l -> (
l.getId().getBorrower().getId() == 1
&&
l.getId().getBook().getId() == 1
&&
l.getDateIn() == null))
.collect(Collectors.toList());
assertThat(bookLoans.size() > 0);
}
}
When the tests run, the console prints:
org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND "Book with ID 2 is not available at branch with ID 2."
Why is the test passing even though the wrong response is being returned?