title: Mocking Spring beans
body: <p>I'd like to continue <a href="https://stackoverflow.com/q/2457239/306488">this</a> question.</p>
  
  <p>These answers <a href="https://stackoverflow.com/questions/2457239/injecting-mockito-mocks-into-a-spring-bean/2461385#2461385">foo</a> and <a href="https://stackoverflow.com/questions/2457239/injecting-mockito-mocks-into-a-spring-bean/3512926#3512926">bar</a> are exactly what I would need.</p>
  
  <p>But for the <a href="https://stackoverflow.com/questions/2457239/injecting-mockito-mocks-into-a-spring-bean/3512926#3512926">bar</a> example spring doesn't infer the type of bean that is returned from generic mock() method. But actually it's exactly the same as in the MockFactoryBean case, that is working.</p>
  
  <pre><code>class MockFactoryBean&lt;T&gt; implements FactoryBean&lt;T&gt; {
  
      private Class&lt;T&gt; type;
  
      public void setType(final Class&lt;T&gt; type) {
          this.type = type;
      }
  
      @Override
      public T getObject() throws Exception {
          return (T) Mockito.mock(type);
      }
  
      @Override
      public Class&lt;T&gt; getObjectType() {
          return type;
      }
  
      @Override
      public boolean isSingleton() {
          return true;
      }
  }
  </code></pre>
  
  <p>plus</p>
  
  <pre><code>&lt;bean id="mockFactory" name="someType" class="com.example.MocksFactory" &gt;
      &lt;property name="type" value="com.example.SomeType" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>BUT using <code>org.mockito.Mockito</code> and <code>mock()</code> factory method doesn't work</p>
  
  <pre><code>&lt;bean id="dao" class="org.mockito.Mockito" factory-method="mock"&gt; 
      &lt;constructor-arg value="com.example.SomeType" /&gt; 
  &lt;/bean&gt; 
  </code></pre>
  
  <p>and one gets No matching bean of type [ com.example.SomeType ] ... when Autowiring.</p>
  
  <p>@Autowired
  public SomeType someType;</p>
  
  <p>Why is that ? </p>
  
  <p>the way with MockFactoryBean is quite inconvenient because if you have to mock many beans, you end up like this :</p>
  
  <pre><code>&lt;bean id="mockFactory1" name="metadataExtractor" class="tests.base.MocksFactory" &gt;
      &lt;property name="type" value="processing.MetadataExtractor" /&gt;
  &lt;/bean&gt;
  &lt;bean id="mockFactory2" name="fileValidator" class="tests.base.MocksFactory" &gt;
      &lt;property name="type" value="validation.file.FileValidator" /&gt;
  &lt;/bean&gt;
  &lt;bean id="mockFactory3" name="documentMatcher" class="tests.base.MocksFactory" &gt;
      &lt;property name="type" value="validation.matching.DocumentMatcher" /&gt;
  &lt;/bean&gt;
  &lt;bean id="mockFactory4" name="uploadMatcher" class="tests.base.MocksFactory"&gt;
      &lt;property name="type" value="validation.matching.UploadMatcher" /&gt;
  &lt;/bean&gt;
  &lt;bean id="mockFactory5" name="tempFileLocalService" class="tests.base.MocksFactory"&gt;
      &lt;property name="type" value="service.TempFileLocalService" /&gt;
  &lt;/bean&gt;
  &lt;bean id="mockFactory6" name="orderLocalService" class="tests.base.MocksFactory"&gt;
      &lt;property name="type" value="service.OrderLocalService" /&gt;
  &lt;/bean&gt;
  &lt;bean id="mockFactory7" name="counterLocalService" class="tests.base.MocksFactory"&gt;
      &lt;property name="type" value="service.CounterLocalService" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Because without id &amp; name it doesn't infer the type.</p>