Reactively Writing Unit Tests for Methods

Learn how to write unit tests in spring boot and test methods using them.

We'll cover the following

Let’s write unit tests to test our methods in this lesson.

Testing a method

Domain objects aren’t too hard to test. Things get trickier when exercising code that interacts with other components. A good example is our InventoryService. This class contains several operations that have business logic and also interact with external collections through repositories.

On top of that, this service is the first level that has asynchronous, non-blocking flows, thanks to Project Reactor. So how exactly do we test that?

Never fear, Spring Boot and Project Reactor provide us with the tools to empower JUnit to verify that all is well.

To sharpen our focus, let’s test InventoryService.addItemToCart(...). We can start by creating a new test class called InventoryServiceUnitTest, like this:

Get hands-on with 1200+ tech skills courses.