Приведенный ниже код может давать либо NullPointerException, либо IllegalArgumentException, поэтому я перехватываю оба, но независимо от того, какой из них я хочу выдать ExceptionWrapper.
public void postCourse(Course course) throws ExceptionWrapper {
CourseEntity entity = new CourseEntity();
try {
entity.setTitle(course.getTitle());
entity.setAcademy(course.getAcademy());
entity.setCourseLevel(course.getLevel());
entity.setDescription(course.getDescription());
entity.setDuration(course.getDuration());
entity.setTrainer(course.getTrainer());
CourseEntity result = courseRepository.save(entity);
} catch (IllegalArgumentException | NullPointerException ex) {
throw new ExceptionWrapper(HttpStatus.BAD_REQUEST, ex,
FriendlyExceptionMessages.COURSE_OBJECT_IS_NULL.description());
}
}
В моем тесте JUnit 5 я ожидаю, что будет выдан ExceptionWrapper.class, если я передам метод; postCourse () с нулевым объектом. Однако это вызывает у меня исключение NullPointerException.
@Test
public void testPostCourseException() {
Course course = null;
Assertions.assertThrows(ExceptionWrapper.class, () -> {
courseService.postCourse(course);
});
}
Пожалуйста, сообщите, спасибо.