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
From the course: Complete Guide To Java Testing with JUnit 5 & 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
-
-
-
-
-
-
-
-
-
(Locked)
Spy on real objects with Mockito4m 31s
-
(Locked)
Test exception handling with Mockito2m 10s
-
(Locked)
Test void methods with Mockito5m 10s
-
(Locked)
Verify no (or a set number of) interactions5m 1s
-
(Locked)
GetAllValues with ArgumentCaptor5m 1s
-
(Locked)
Test asynchronous code with Mockito3m 17s
-
(Locked)
Mock a static method with Mockito3m 15s
-
(Locked)
Challenge: Test an ordering service1m 2s
-
(Locked)
Solution: Test an ordering service8m 21s
-
(Locked)
-
-