From the course: Complete Guide To Java Testing with JUnit 5 & Mockito

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Test asynchronous code with Mockito

Test asynchronous code with Mockito

- [Instructor] Asynchronous programming has become increasingly common in modern software development, especially with the rise of reactive programming. Testing asynchronous code can be challenging, but with the right tools and techniques, it can be made simpler and more effective. Let's take a look at an example. Here, we have a user service class that interacts asynchronously with a user repository. Completable Future was introduced in Java eight, and represents a future result of an asynchronous operation. Here, that operation is finding a user by its ID. The return type indicates it will eventually provide a user object as a result of the asynchronous operation. The supply async method is a static factory method provided by the completable feature class. It will execute a supplier asynchronously. Here, that supplier is our Lambda function. When supply async is called, the logic inside the Lambda expression will be submitted to the default executor service. This means the method…

Contents