I need to test @ControllerAdvice methods which are in service layer. But I faced two issues:
- ExceptionHandlers not triggered
- And even if it would be triggered, I find out that don't now how to test it)
@Slf4j @ControllerAdvice public class AppExceptionHandler {
@ExceptionHandler(value = {.class})
public ResponseEntity<Object> handleMyException(MyException ex, WebRequest request) {
ErrorMessage errorMessage = ErrorMessage.builder()
.message(ex.getMessage())
.httpStatus(HttpStatus.BAD_REQUEST)
.time(ZonedDateTime.now())
.build();
return new ResponseEntity<>(errorMessage, HttpStatus.BAD_REQUEST);
}
@RunWith(MockitoJUnitRunner.class)
@RequiredArgsConstructor
public class MyExceptionTest {
private final AppExceptionHandler handler;
@Mock private final MyService service;
@Test
public void test() throws MyException {
when(service.create(any()))
.thenThrow(MyException .class);
}
}
Aucun commentaire:
Enregistrer un commentaire