title: Spring Bean Injection Failing Due To Proxy
body: <p><strong>Spring Version</strong>: 3.2.4.RELEASE and 3.2.9.RELEASE</p>
  
  <p><strong>Mockito Version</strong>: 1.8.5</p>
  
  <p>I've been trying to introduce H2 tests to an old project for integration testing, and I've been running into a few issues. Due to the way transactions were propagating, I needed to mock out an autowired class. I've done this before, but I'm now running into severe problems. The following error message is being thrown when initialising the test:</p>
  
  <blockquote>
    <p>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.stuff.XMLITCase': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: <strong>Bean named 'TheProcessor' must be of type [com.stuff.XMLBatchFileProcessor], but was actually of type [$Proxy118]</strong>
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)</p>
  </blockquote>
  
  <p>Diving into this a bit deeper, it turns out the the bean is in-fact a proxy. If we check the AbstractBeanFactory (round line 239), we can see the proxy:</p>
  
  <blockquote>
    <p>sharedInstance = {$Proxy117@7035} "com.stuff.XMLBatchFileProcessor@66c540d0"
     h = {org.springframework.aop.framework.JdkDynamicAopProxy@7039} </p>
  </blockquote>
  
  <p>The only problem is, I've no clue where this is coming from. I've gone over the config and dependencies, and can't find anywhere that this should be happening.</p>
  
  <h2>Project Setup</h2>
  
  <p>Unfortunately I can't give a sample project for this, but I'll go over my test configuration. I have a root class that I extend for the tests:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath:/spring/spring-test-context.xml"})
  @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
  public abstract class AbstractIntegrationTest {
  }
  </code></pre>
  
  <p>This simply loads in some spring config and rolls back the transactions after each test.</p>
  
  <p>The spring config is nothing strange either, though there is one difference between my other module and this one. This is the transaction manager and session factory:</p>
  
  <pre><code>&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="hibernateSessionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
  ...
  &lt;/bean&gt;
  </code></pre>
  
  <p>In my other module, I'm using an entityManagerFactory, and a different transaction manager:</p>
  
  <pre><code>&lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
  ...
  &lt;/bean&gt;
  </code></pre>
  
  <p>The actually class has some autowired fields, and the usual @Service annotation:</p>
  
  <pre><code>@Service(value = "TheProcessor")
  public final class XMLBatchFileProcessor extends BatchFileProcessor implements IXMLBatchProcessor {
  </code></pre>
  
  <p>Finally, the actual test is as follows:</p>
  
  <pre><code>public class XMLITCase extends AbstractIntegrationTest {
  
      @Resource(name = "TheProcessor")
      @InjectMocks
      private XMLBatchFileProcessor xmlProcessor;
  
      @Mock
      private ProcessHelper processHelper;
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      public void test() throws Exception {
          Assert.assertNotNull(xmlProcessor);
      }
  
  }
  </code></pre>
  
  <p>If I replace the XMLBatchFileProcessor with the interface and autowire the field, then there aren't any problems compiling. Mockito, however, never replaces the autowired bean with the mocked object. If it did, then I wouldn't bother with the @Resource annotations and naming the service, thus avoiding the Proxy issue.</p>
  
  <p>Any assistance on this would be appreciate. I'll be focusing on the session factory and the differences there, but it's quite possible that I'm missing something else entirely.</p>
  
  <h2>EDIT</h2>
  
  <p>Going on Sotirios' comment, I had another look this morning and indeed had missed that the xmlProcessor has a <code>@Transactional</code> annotation, thus meaning that the class needs to be proxied. If I remove the <code>final</code> declaration and let CGLib enhance it, then Mockito does replace the bean when <code>initMocks(this)</code> this called. When a method is called, however, CGLib seems to replace all the beans with Spring enhanced versions, hence overwriting the Mockito version.</p>
  
  <p>What is the correct way to use both Mockito and Spring in an integration test for a class with <code>@Transactional</code> annotations?</p>