lundi 7 mai 2018

Test RestController Delete Crud method return 404 expected 400

I have problem with test method. I want to test delete metod ( shouldNotDeletePersonByGivenId) but test dosen't work. This is My test:

@Test
public void shouldNotDeletePersonByGivenId() throws Exception {
    Mockito.doThrow(new PersonService.NoEntityFoundException()).when(personService).deleteById(1L);

    mockMvc.perform(delete("/persons/{id}", 1))
            .andExpect(status().isBadRequest());
}

In service i have this method:

public static class NoEntityFoundException extends RuntimeException {
    public NoEntityFoundException() {
        super("There is no Entity in database with given id.");
    }

}

When i start test i have request : java.lang.AssertionError: Status Expected :400 Actual :404

In restController my delete method look's like this :

@DeleteMapping("/persons/{id}")
    public ResponseEntity<?> deleteById(@PathVariable Long id) {
        try {
            personService.deleteById(id);
            return ResponseEntity.ok().body("{Deleted}");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Cant delete! Entity not exist");
        }
    }

Aucun commentaire:

Enregistrer un commentaire