课程: Complete Guide To Java Testing with JUnit 5 & Mockito

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

Spy on real objects with 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…

内容