Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring webflux unit test method which returns Mono/Flux

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?

like image 989
pvpkiran Avatar asked Sep 14 '25 05:09

pvpkiran


1 Answers

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
like image 93
Martin Tarjányi Avatar answered Sep 16 '25 08:09

Martin Tarjányi