Estou enfrentando um erro que não entendi como resolver:
Tenho esse método e quero testar se está salvando o course.
@Override
@Transactional
public CourseCreateResponse createCourse(CourseCreateRequest request){
createValidators.forEach(v -> v.validate(request));
var instructor = userRepository.getReferenceById(request.instructorId());
var course = new Course(request);
course.setInstructor(instructor);
return new CourseCreateResponse(courseRepository.save(course));
}
Criei o seguite teste
@ExtendWith(MockitoExtension.class)
public class CourseServiceTestB {
@InjectMocks
private CourseServiceImpl service;
@Mock
private UserRepository userRepository;
@Mock
private CourseRepository courseRepository;
@Mock
private User instructor;
@Mock
private Course course;
private CourseCreateRequest request;
@Captor
private ArgumentCaptor<Course> courseCaptor;
@Test
void shoulSaveCourse() {
this.request = new CourseCreateRequest("curso A", "curso-a", "Curso A", 1L);
given(course.getId()).willReturn(1L);
given(userRepository.getReferenceById(request.instructorId())).willReturn(instructor);
service.createCourse(request);
then(courseRepository).should().save(courseCaptor.capture());
var savedCourse = courseCaptor.getValue();
Assertions.assertEquals(instructor, savedCourse.getInstructor());
Assertions.assertEquals(request.name(), savedCourse.getName());
Assertions.assertEquals(request.code(), savedCourse.getCode());
Assertions.assertEquals(request.description(), savedCourse.getDescription());
Assertions.assertEquals(Status.ACTIVE, savedCourse.getStatus());
Assertions.assertEquals(LocalDate.now(), savedCourse.getCreatedAt());
}
E estou tomando esse erro:
java.lang.NullPointerException: Cannot invoke "io.github.enrolmentsystem.domain.course.Course.getId()" because "course" is null
at io.github.enrolmentsystem.domain.course.response.CourseCreateResponse.<init>(CourseCreateResponse.java:8)
at io.github.enrolmentsystem.service.impl.CourseServiceImpl.createCourse(CourseServiceImpl.java:46)
at io.github.enrolmentsystem.course.service.CourseServiceTestB.shoulSaveCourse(CourseServiceTestB.java:60)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)