@Test @SuppressFBWarnings(value = "DM_NEW_FOR_GETCLASS", justification = "It's needed for the test") void canHandleMethodArgumentNotValidExceptions() { final Method method = new Object() { }.getClass().getEnclosingMethod(); final MethodParameter parameter = Mockito.mock(MethodParameter.class); Mockito.when(parameter.getMethod()).thenReturn(method); final Executable executable = Mockito.mock(Executable.class); Mockito.when(parameter.getExecutable()).thenReturn(executable); Mockito.when(executable.toGenericString()).thenReturn(UUID.randomUUID().toString()); final BindingResult bindingResult = Mockito.mock(BindingResult.class); Mockito.when(bindingResult.getAllErrors()).thenReturn(Lists.newArrayList()); final MethodArgumentNotValidException exception = new MethodArgumentNotValidException( parameter, bindingResult ); final ResponseEntity<GeniePreconditionException> response = this.mapper.handleMethodArgumentNotValidException(exception); Assertions.assertThat(response.getStatusCode()).isEqualByComparingTo(HttpStatus.PRECONDITION_FAILED); Mockito .verify(this.registry, Mockito.times(1)) .counter( GenieExceptionMapper.CONTROLLER_EXCEPTION_COUNTER_NAME, Sets.newHashSet( Tag.of(MetricsConstants.TagKeys.EXCEPTION_CLASS, exception.getClass().getCanonicalName()) ) ); Mockito .verify(this.counter, Mockito.times(1)) .increment(); }
@ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException( final MethodArgumentNotValidException e ) { this.countExceptionAndLog(e); return new ResponseEntity<>( new GeniePreconditionException(e.getMessage(), e), HttpStatus.PRECONDITION_FAILED ); }
GenieExceptionMapper { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException( final MethodArgumentNotValidException e ) { this.countExceptionAndLog(e); return new ResponseEntity<>( new GeniePreconditionException(e.getMessage(), e), HttpStatus.PRECONDITION_FAILED ); } }
GenieExceptionMapper { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException( final MethodArgumentNotValidException e ) { this.countExceptionAndLog(e); return new ResponseEntity<>( new GeniePreconditionException(e.getMessage(), e), HttpStatus.PRECONDITION_FAILED ); } @Autowired  GenieExceptionMapper(final MeterRegistry registry); }
GenieExceptionMapper { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException( final MethodArgumentNotValidException e ) { this.countExceptionAndLog(e); return new ResponseEntity<>( new GeniePreconditionException(e.getMessage(), e), HttpStatus.PRECONDITION_FAILED ); } @Autowired  GenieExceptionMapper(final MeterRegistry registry); @ExceptionHandler(GenieException.class) ResponseEntity<GenieException> handleGenieException(final GenieException e); @ExceptionHandler(GenieRuntimeException.class) ResponseEntity<GenieRuntimeException> handleGenieRuntimeException(final GenieRuntimeException e); @ExceptionHandler(GenieCheckedException.class) ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e); @ExceptionHandler(ConstraintViolationException.class) ResponseEntity<GeniePreconditionException> handleConstraintViolation(
        final ConstraintViolationException cve
    ); @ExceptionHandler(MethodArgumentNotValidException.class) ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException(
        final MethodArgumentNotValidException e
    ); }
GenieExceptionMapper { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException( final MethodArgumentNotValidException e ) { this.countExceptionAndLog(e); return new ResponseEntity<>( new GeniePreconditionException(e.getMessage(), e), HttpStatus.PRECONDITION_FAILED ); } @Autowired  GenieExceptionMapper(final MeterRegistry registry); @ExceptionHandler(GenieException.class) ResponseEntity<GenieException> handleGenieException(final GenieException e); @ExceptionHandler(GenieRuntimeException.class) ResponseEntity<GenieRuntimeException> handleGenieRuntimeException(final GenieRuntimeException e); @ExceptionHandler(GenieCheckedException.class) ResponseEntity<GenieCheckedException> handleGenieCheckedException(final GenieCheckedException e); @ExceptionHandler(ConstraintViolationException.class) ResponseEntity<GeniePreconditionException> handleConstraintViolation(
        final ConstraintViolationException cve
    ); @ExceptionHandler(MethodArgumentNotValidException.class) ResponseEntity<GeniePreconditionException> handleMethodArgumentNotValidException(
        final MethodArgumentNotValidException e
    );  }