Alpesh Jikadra
Alpesh Jikadra

Reputation: 1722

Spring Webflux how to verify that all steps are executed successfully

I am new to Spring webflux, I have just started webflux project and I am stuck at one place.

I am creating a student object with Id and FileName.

When I call a deleteById(Long) method on studentService, then I want to first delete the file from storage and then I want to delete record from Repository,

And all these methods are returning the Mono

Below are my code for student with Service and Repository

public class Student {
  private Long id;
  private String fileName;

  //getter & setter

}


public class StudentRepository {

    public Mono<Student> findById(long l){
        Student sample = new Student();
        sample.setFileName("file-name");
        sample.setId(1L);
        return Mono.just(sample);
    }

    public Mono<Void> deleteFile(String fileName) {
        return Mono.empty();
    }
    public Mono<Void> deleteById(Long id) {
        return Mono.empty();
    }
}


public class StudentService {

    private StudentRepository repository;

    public void setRepository(StudentRepository repository) {
        this.repository = repository;
    }

    public Mono<Student> findById(long l){
        Student sample = new Student();
        sample.setFileName("file-name");
        sample.setId(1L);
        return Mono.just(sample);
    }
    public Mono<Void> deleteById(Long id){
        return repository.findById(id)
                .flatMap(student ->
                        repository.deleteFile(student.getFileName()).thenReturn(student)
                )
                .flatMap(studentMono ->
                        repository.deleteById(studentMono.getId())
                );

    }
}

Now I want to verify that first file is deleted from File storage and then record should be delete from DB.

I wrote test like below

public void test(){

    StudentService studentService = new StudentService();
    StudentRepository studentRepository = mock(StudentRepository.class);
    studentService.setRepository(studentRepository);
    Student student = new Student();
    student.setFileName("file-name");
    student.setId(1L);
    Mono<Student> studentMono = Mono.just(student);
    Mockito.when(studentRepository.findById(1L)).thenReturn(studentMono);

    studentService.deleteById(1L);

    StepVerifier.create(studentMono)
            .expectNextMatches(leadImport->leadImport.getFileName().equals("file-name"))
            .expectNextMatches(leadImport -> leadImport.getId() == 1L)
            .verifyComplete();
  }

But some how my test is failing.

Can someone please help me how to verify all my expected steps like

Upvotes: 1

Views: 3997

Answers (1)

sam3131
sam3131

Reputation: 397

you create a StepVerifier from a Mono.just(student), it means you are watching a single element

chaining the expectNextMatches will work if you have more than one element : Flux.just(student1, student2,..) as it will verify each student for each calls

change your code by calling expectNext only once :

StepVerifier.create(studentMono)
            .expectNextMatches(leadImport->leadImport.getFileName().equals("file-name") && leadImport.getId() == 1L)
            .verifyComplete();

Upvotes: 1

Related Questions