In the controller advice class you can handle. @ExceptionHandler(CustomException.class) public ResponseEntity customException(CustomException exception) { return new ResponseEntity( new CustomErrorModel(), HttpStatus.BAD_REQUEST ); }
In case the custom handler does not take effect, add @Order(-2) on your handler class to give it a higher priority than the DefaultErrorWebExceptionHandler.
It should go last. From JavaDoc of @Order: Lower values have higher priority. The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).
It’s really helpful but can you help in writing unit test cases in JUnit 5 so that coverage will increase for these 2 classes ?
How to handle if we have different custom exceptions
In the controller advice class you can handle.
@ExceptionHandler(CustomException.class) public ResponseEntity customException(CustomException exception) { return new ResponseEntity( new CustomErrorModel(), HttpStatus.BAD_REQUEST ); }
In case the custom handler does not take effect, add @Order(-2) on your handler class to give it a higher priority than the DefaultErrorWebExceptionHandler.
It should go last. From JavaDoc of @Order: Lower values have higher priority. The default value is Ordered.LOWEST_PRECEDENCE, indicating lowest priority (losing to any other specified order value).
Is there any JUnit5 test cases for coverage of these 2 classes ?
Need it kinda urgently, if you could please help
No I have not written