From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Spy on real objects with Mockito
From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Spy on real objects with Mockito
- [Instructor] With a good understanding of Mockito and what mocking means, we can begin to dive into some more advanced concepts. One of these advanced concepts is partial mocking through something called spying. Partial mocking allows you to create an object that combines real method calls and stubbed methods. This can be really useful when you want to test certain parts of an object's behavior while controlling or isolating other parts. In Mockito, this can be achieved using a spy. Let's take a look at an example. Here we have a payment service that uses an account service as a dependency. Instead of mocking the account service, we use the spy annotation so we can use most of its behavior and only mock certain parts. To stub the account service, we use the doReturn method because it ensures that the real method is not called during the stubbing process. It makes it clear that this is the part of the spy we're mocking and avoids any unintended side effects. Everything else is pretty…
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)
-
-