I have the following code:
@DeleteMapping(value = ["/library/books/{id}"])
fun deleteBook( @PathVariable("id") id: kotlin.Int): ResponseEntity<Unit> {
return ResponseEntity(service.deleteBook(id), HttpStatus.valueOf(200))
}
override fun deleteBook(id: Int) {
libraryRepository.findById(id).let {
if(it == null){
throw NotFoundException("Book with the given id not found")
}
libraryRepository.delete(it)
}
}
@Test
fun `delete book and return 200`(){
val library = Library(name = "testName", author = "testAuthor")
`when`(libraryRepository.findById(1)).thenReturn(library)
mvc.perform(MockMvcRequestBuilders.delete("/library/books/1"))
.andExpect(MockMvcResultMatchers.status().isOk)
verify(libraryRepository).delete(library)
}
Here I am trying to write test, I suppose that it should fail, but it passed. I don't understand why my test is working, although I do not imitate when`(libraryRepository.delete(...)).