Working with Mockito – a popular mocking library
The previous sections have shown examples of using stubs and mocks to test code. We have been writing these test doubles by hand. It’s obviously quite repetitive and time-consuming to do this. It begs the question of if this repetitive boilerplate code can be automated away. Thankfully for us, it can. This section will review the help available in the popular Mockito library.
Mockito is a free-of-charge open source library under the MIT license. This license means we can use this for commercial development work, subject to agreement by those we work for. Mockito provides a large range of features aimed at creating test doubles with very little code. The Mockito website can be found at https://site.mockito.org/.
Getting started with Mockito
Getting started with Mockito is straightforward. We pull in the Mockito
library and an extension library in our Gradle file. The extension library allows Mockito
to integrate...