Creating mocks with custom configuration
Even though in the majority of cases you will not need the feature discussed in the preceding recipe, sometimes you may want your mock to satisfy some additional prerequisites. Thanks to Mockito's Mockito.withSettings
fluent interface, you can easily set up your custom MockitoSettings
object that you can pass to the Mockito.mock
method that will create your mock. When you check out the Javadoc of MockitoSettings
, you will see a note that you shouldn't use that class too often. That's good advice, because you should make it a practice to write your code and tests in such a way that it is either of high quality or low complexity. In other words, in real life, you shouldn't need to configure your mocks in such a complex way.
Getting ready
Let's take a look at the following MockitoSettings
interface methods:
extraInterfaces(...)
: This method specifies which additional interfaces the mock should implement. It can be quite useful when dealing with legacy...