title: Injecting Mockito mocks into a Spring bean
body: <p>I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the <code>@Autowired</code> annotation on private member fields.</p>
  
  <p>I have considered using <code>ReflectionTestUtils.setField</code> but the bean instance that I wish to inject is actually a proxy and hence does not declare the private member fields of the target class. I do not wish to create a public setter to the dependency as I will then be modifying my interface purely for the purposes of testing.</p>
  
  <p>I have followed some <a href="http://forum.springsource.org/showthread.php?t=58444&amp;page=1" rel="noreferrer">advice</a> given by the Spring community but the mock does not get created and the auto-wiring fails:</p>
  
  <pre><code>&lt;bean id="dao" class="org.mockito.Mockito" factory-method="mock"&gt;
      &lt;constructor-arg value="com.package.Dao" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The error I currently encounter is as follows:</p>
  
  <pre><code>...
  Caused by: org...NoSuchBeanDefinitionException:
      No matching bean of type [com.package.Dao] found for dependency:
      expected at least 1 bean which qualifies as autowire candidate for this dependency.
      Dependency annotations: {
          @org...Autowired(required=true),
          @org...Qualifier(value=dao)
      }
  at org...DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(D...y.java:901)
  at org...DefaultListableBeanFactory.doResolveDependency(D...y.java:770)
  </code></pre>
  
  <p>If I set the <code>constructor-arg</code> value to something invalid no error occurs when starting the application context.</p>