Mocking dependencies using Mockito
In this section, we will learn about the basic fundamentals in relation to mocking dependencies (or, using test doubles) within a class/function. As explained in one of the preceding sections, some of the key building blocks of unit tests are spies, mocks, stubs, fakes, and so on. There are different Java mocking frameworks which provide these features. In this section, we will look into Mockito and related concepts.
The following are key considerations when using mocking:
- Identify objects in the block of code (class/methods under test) which need to be mocked or spied. These could be data access objects (DAOs), services, and so on.
- Create mock objects.
- Pre-program the behavior of mock objects.
- Execute the code or invoke the function/method under test.
- Verify the behavior or examine the function/method arguments/return objects based on whether a mock or spy was used, respectively.
Getting set up with Mockito
In order to get set up with Mockito, one would require...