I have this test:
@Test
@DisplayName("Add student with studentId provided in input")
void addStudentOkStudentId() {
UUID uuid = UUID.randomUUID();
String studentId = "studentId-custom";
AddStudentRequest input = StudentMother.createAddStudentRequest()
.studentId(studentId).build();
StudentIdentifier studentIdentifier = new StudentIdentifier();
studentIdentifier.setValue(studentId);
Student expectedStudentToSave = buildStudent(uuid, input);
expectedStudentToSave.setStudentId(studentIdentifier);
Student repoResponse = buildStudent(uuid, input);
repoResponse.setStudentId(studentIdentifier);
repoResponse.setId(1L);
AddStudentResponse expectedResponse = mapper.toAddStudentResponse(repoResponse);
when(uuidGeneratorMock.generate()).thenReturn(uuid.toString());
when(studentRepositoryMock.addStudent(any(Student.class))).thenReturn(Mono.just(repoResponse));
StepVerifier.create(addStudent.execute(input))
.expectNext(expectedResponse)
.verifyComplete();
verify(studentRepositoryMock, times(1)).addStudent(argThat(
student -> {
Assertions.assertThat(student).isEqualTo(expectedStudentToSave);
return true;
}
));
verify(studentRepositoryMock, times(0)).getNextStudentIdentifier();
}
private static Student buildStudent(UUID uuid, AddStudentRequest request) {
return Student.builder()
.uuid(uuid.toString())
.studentId(new StudentIdentifier(42))
.email(request.email())
.firstName(request.firstName())
.lastName(request.lastName())
.build();
}
This is the code:
private final StudentRepositoryPort studentRepository;
private final StudentValidator validator;
private final StudentMapper studentMapper;
private final UuidGenerator uuidGenerator;
@Override
public Mono<AddStudentResponse> execute(AddStudentRequest request) {
return validateRequest(request)
.flatMap(this::createStudentFromRequest)
.flatMap(this::ensureStudentIdentifier)
.flatMap(this::persistStudent)
.map(this::toResponse);
}
private Mono<AddStudentRequest> validateRequest(AddStudentRequest request) {
return Mono.defer(() -> {
List<String> errors = validator.validateAddStudent(request);
return !errors.isEmpty()
? Mono.error(new ValidationException(StudentError.ADD_STUDENT_VALIDATION, errors))
: Mono.just(request);
});
}
private Mono<Student> createStudentFromRequest(AddStudentRequest request) {
return Mono.fromCallable(() -> {
Student student = studentMapper.toStudent(request);
student.setUuid(uuidGenerator.generate()); // Set diretto
return student;
});
}
private Mono<Student> ensureStudentIdentifier(Student student) {
return Mono.fromCallable(student::getStudentId)
.filter(id -> StringUtils.isNotBlank(id.getValue()))
.map(__ -> student)
.switchIfEmpty(
generateStudentIdentifier().map(id -> {
student.setStudentId(id); // Modifica dello stato esistente
return student;
})
);
}
private Mono<StudentIdentifier> generateStudentIdentifier() {
return studentRepository.getNextStudentIdentifier()
.map(id -> new StudentIdentifier(Math.toIntExact(id)));
}
private Mono<Student> persistStudent(Student student) {
return studentRepository.addStudent(student);
}
private AddStudentResponse toResponse(Student student) {
return studentMapper.toAddStudentResponse(student);
}
The test tries to generate the student identifier even though it's is setted in the Student obj (I can see it in debug). This cause an NPE because the method to generate the student id (getNextStudentIdentifier()) is not mocked, rightfully since should not be executed in this execution flow.
If I start the app and run a request with postman, the code to generate the studentId is not invoked (expected behaviour). Why in the test it tries to generate the studentId? Can't figure it out, I know it's something to do with reactor but I can't...
The question is why this filter gets a mono.empty:
Mono.fromCallable(student::getStudentId)
.filter(id -> StringUtils.isNotBlank(id.getValue()))
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745251141a4618683.html
评论列表(0条)