title: Mock services inside another spring service with mockito
body: <p>I'm facing problems mocking services injected inside of other services within the Spring framework. Here is my code:</p>
  
  <pre><code>@Service("productService")
  public class ProductServiceImpl implements ProductService {
  
      @Autowired
      private ClientService clientService;
  
      public void doSomething(Long clientId) {
          Client client = clientService.getById(clientId);
          // do something
      }
  }
  </code></pre>
  
  <p>I want to mock the <code>ClientService</code> inside my test, so I tried the following:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:/spring-config.xml" })
  public class ProductServiceTest {
  
      @Autowired
      private ProductService productService;
  
      @Mock
      private ClientService clientService;
  
      @Test
      public void testDoSomething() throws Exception {
          when(clientService.getById(anyLong()))
                  .thenReturn(this.generateClient());
  
          /* when I call this method, I want the clientService
           * inside productService to be the mock that one I mocked
           * in this test, but instead, it is injecting the Spring 
           * proxy version of clientService, not my mock.. :(
           */
          productService.doSomething(new Long(1));
      }
  
      @Before
      public void beforeTests() throws Exception {
          MockitoAnnotations.initMocks(this);
      }
  
      private Client generateClient() {
          Client client = new Client();
          client.setName("Foo");
          return client;
      }
  }
  </code></pre>
  
  <p>The <code>clientService</code> inside <code>productService</code> is the Spring proxy version, not the mock that I want. Is it possible to do what I want with Mockito?</p>