title: How to mock only one method of one injected dependency and inject the rest normally?
body: <p>I'm writing tests for a project with a heavy use of dependency injection.</p>
  
  <p>So normally I would just inject the object that I'm testing:</p>
  
  <pre><code>public class RegistrationTest
          extends WithApplication {
  
      private RegistrationController controller;
  
      @Before
      public void setUp() throws Exception {
          Injector injector = app.injector();
          controller = injector.instanceOf(RegistrationController.class);
      }
  
      @Test
      public void openRegistrationView() {
          Result result = controller.registrationForm();
          assertEquals(OK, result.status());
      }
  }
  </code></pre>
  
  <p>Now I need to mock a return value of a method of one of the dependencies of the class under test.</p>
  
  <p>To do this I can use <code>@InjectMocks</code> and <code>@RunWith(MockitoJUnitRunner.class)</code> to inject mocked dependencies.</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class RegistrationTest
          extends WithApplication {
  
      @InjectMocks
      private RegistrationController controller;
      @Mock
      private FormFactory formFactory;
      @Mock
      private RegistrationIpCache registrationIpCache;
  
      @Test
      public void openRegistrationView() {
          doReturn("test").when(registrationIpCache).getLast();
          Result result = controller.registrationForm();
          assertEquals(OK, result.status());
      }
  }
  </code></pre>
  
  <p>But while I only want to mock one method call of <code>RegistrationIpCache</code> I need to mock the whole class and also I need to mock all other dependencies of <code>RegistrationController</code> like <code>FormFactory</code> in my example. Now every method of all mocked classes doesn't work as expected.</p>
  
  <p>Sure I can use <code>doCallRealMethod()</code> to mock every method of all dependencies</p>
  
  <pre><code>doCallRealMethod().when(formFactory).form(Registration.class);
  </code></pre>
  
  <p>But as you already can imagine the work and the amount of boilerplate code needed for this to work is extreme.</p>
  
  <p><strong>Isn't it possible to inject my class normally and then only spy one dependency or mock one method of one dependency?</strong></p>