### Question:
RestExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler({ ConstraintViolationException.class }) protected ResponseEntity<Object> handle(ConstraintViolationException ex, WebRequest request) { List<String> errors = new ArrayList<>(); for (ConstraintViolation<?> violation : ex.getConstraintViolations()) { errors.add(violation.getPropertyPath() + ": " + violation.getMessage()); } log.error("Constraint exception: {}" + errors); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); ApiError apiError = ApiError.builder() .status(HttpStatus.BAD_REQUEST) .message(ExceptionUtils.getRootCauseMessage(ex)) .errors(errors) .build(); return handleExceptionInternal(ex, apiError, headers, HttpStatus.BAD_REQUEST, request); }    }### Answer:
@Test void shouldHandleConstraintException() { Set<ConstraintViolation<String>> constraints = new HashSet<>(); constraints.add(constraintViolation); ConstraintViolationException constraintViolationException = new ConstraintViolationException( constraints); given(constraintViolation.getPropertyPath()).willReturn(createPathFromString("testField")); given(constraintViolation.getMessage()).willReturn("testMessage"); ResponseEntity<Object> response = restExceptionHandler .handle(constraintViolationException, webRequest); then(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); then(response.getBody()).satisfies(body -> { ApiError error = (ApiError) body; then(error.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST); then(error.getMessage()).contains("ConstraintViolationException"); then(error.getErrors().get(0)).isEqualTo("testField: testMessage"); }); }