Hej chciałem napisać test metody zmieniającej hasło użytkownika. Metoda działa poprawnie testując ją w postmanie i test jest poprawny w momencie kiedy nie testuje wyjątku.
public String updateUserPassword(UpdatePasswordDto user)
{
try {
UserDetails userDetails= manager.loadUserByUsername(user.getUserName());
manager.updatePassword(userDetails,encoder.encode(user.getPassword()));
return user.getUserName();
}
catch (UsernameNotFoundException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
}
@PutMapping
public ResponseEntity<String> updateUser(@RequestBody UpdatePasswordDto user)
{
HttpHeaders headers= new HttpHeaders();
try{
String userName=userService.updateUserPassword(user);
headers.add("successfull","true");
return new ResponseEntity<>(user.getUserName(),headers, HttpStatus.OK);
}
catch (ResponseStatusException e)
{
headers.add("successfull","false");
return new ResponseEntity<>("No such a user found",headers, HttpStatus.BAD_REQUEST);
}
}
@Test
@WithMockUser(username = "admin", authorities = {"ADMIN"})
public void shouldThrowExceptionUpdatePassword() throws Exception {
UpdatePasswordDto pass=new UpdatePasswordDto("Test","Jerzy");
when(userService.updateUserPassword(pass))
.thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND));
MvcResult s = mvc.perform(put("/api/user")
.header("indicatorcolor","green")
.contentType(MediaType.APPLICATION_JSON)
.content(asJsonString(pass)))
.andExpect(status().is(400))
.andReturn();
}
}
Kompletnie nie mam pojęcia co jest źle, a pewnie jest to oczywisty błąd.