title: Mockito - Creating nested mock objects
body: <p>I'm learning Mockito. I am facing problem while creating mock for nested objects. See</p>
  
  <pre><code>public interface BaseManager {
      public Query createQuery(String queryString);
  }
  </code></pre>
  
  <p>and an implementation class for that</p>
  
  <pre><code>public class BaseManagerImpl implements BaseManager {
      @Autowired
      private SessionFactory sessionFactory;
      // ...
  }
  </code></pre>
  
  <p>Module level hibernate manager, for example:</p>
  
  <pre><code>public interface RegistrationManager {
      @Transactional
      public List&lt;Country&gt; getCountries();
  }
  </code></pre>
  
  <p>and an implementation class for that</p>
  
  <pre><code>public class RegistrationManagerImpl implements RegistrationManager {
      @Autowired
      private BaseManager baseManager;
      // ...
  }
  </code></pre>
  
  <p>Now I'm facing problem in creating mocked base manager. My test class is:</p>
  
  <pre><code> public class MockitoTest {
      private RegistrationManager registrationManager = new RegistrationManagerImpl();
  
      @Mock private BaseManager baseManager;
  
      @Mock private SessionFactory sessionFactory;
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
      //  baseManager.setSessionFactory(sessionFactory);
          registrationManager.setBaseManager(baseManager);
      }
      // ...
      // @Test
  }
  </code></pre>
  
  <p>Problem: <strong>sessionFactory is not instantiated inside baseManager.</strong>
  Please help creating mock object. Thanks in advance!</p>