Unit testing With BlockHound
Explore how to use BlockHound within Spring Boot unit tests to identify blocking calls in reactive programming. Learn to set up BlockHound, mock dependencies, and write tests that catch improper blocking in reactive streams, improving application performance.
We'll cover the following...
Slipping BlockHound into the main entry point for our application is an effective warmup, but it’s not the best strategy. That approach leaves BlockHound active in production.
It’s better to leverage BlockHound in our test cases.
Add BlockHound to our application
To add BlockHound’s JUnit support, we add the following to our pom.xml file:
This is required to bring in BlockHound and support for its JUnit Platform, TestExecutionListener, meaning that it registers itself to look for blocking calls inside test methods.
What, exactly, is BlockHound looking for from a test-case perspective? Let’s start with a somewhat contrived example:
Here’s a breakdown of the code above:
-
In line 6, we insert a deliberate delay that forces this whole flow onto a Reactor thread, putting it into BlockHound’s scope.
-
In line 9,
Thread.sleepis a blocking call because ...