2
votes

I am using spring webflux in my project. My controller class calls service class methods which returns Mono or Flux.

I am trying to write unit tests for my service class. I am not sure how to write unit tests for a method which returns Mono/Flux. Most of the articles I checked suggested me to use WebclientTest. But the point is, I am testing my service class here. I have used WebclientTest when I tested my web layer(controller class) by mocking service class methods.

Now I want to write unit tests for my service class methods(by mocking database class)

Any ideas on how to achieve this? Should I use call the service class method from test and call block() or is there a better way?

1

1 Answers

2
votes

You can use StepVerifier provided by Project Reactor for testing purposes.

Although, for simple scenarios (for example when you only have a Mono) a block call will do just fine.

StepVerifier might come in handy when you...

  • have a Flux and want to assert multiple items/events flowing through the pipeline
  • deal with time
  • test Reactor context