Skip to main content

Exception Testing

Learn Exception Testing (Spring MVC Testing, Spring MVC) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Fresher Notes Exception testing checks that errors thrown by controllers or services are turned into the right HTTP status and body, usually by @ExceptionHandler or @RestControllerAdvice . Stub a failure with when(service.find(9L)).thenThrow(new NotFoundException()) and assert status().isNotFound() . @WebMvcTest includes @ControllerAdvice beans automatically, so the real error mapping is exercised.

More lessons in Spring MVC Testing