March 2015
Intermediate to advanced
236 pages
5h 26m
English
Passing a mock to a target class using a constructor is one technique. It requires a change to the interface and exposes a private detail to another class in the production code. Not a great deal, but you can do better by using a dependency injection (DI) tool. You’ll find a handful or more of DI tools out there, including Spring DI and Google Guice.
Because we’re using Mockito, however, we’ll use its built-in DI capabilities. The DI power in Mockito isn’t as sophisticated as you might find in other tools, but most of the time you shouldn’t need anything more.
Using DI in Mockito means following these steps:
Create a mock instance using the @Mock annotation.
Declare a target instance variable ...
Read now
Unlock full access