title: @Autowired with JUnit5
body: <p>I'm using JUnit5 and need to inject a bean managed by Spring (transactionManager), but I can't have the @Autowired work. 
  This is what I did : </p>
  
  <p>First I declared the beans in an xml file : </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://www.springframework.org/schema/beans"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
                             http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"&gt;
  
  
  &lt;!-- Transaction Manager --&gt;
  &lt;bean id="transactionManager" class="com.dummy.myerp.business.impl.TransactionManager"
   factory-method="getInstance"&gt;
  &lt;constructor-arg ref="txManagerMYERP"/&gt;
  &lt;/bean&gt;
  
  
      &lt;!-- PlatformTransactionManager pour le DataSource MYERP --&gt;
      &lt;bean id="txManagerMYERP" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSourceMYERP"/&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="dataSourceMYERP" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;constructor-arg value="javax.sql.DataSource"/&gt;
  
          &lt;property name="driverClassName" value="org.postgresql.Driver" /&gt;
          &lt;property name="url" value="jdbc:postgresql://localhost:9032/db_myerp" /&gt;
          &lt;property name="username" value="usr_myerp"/&gt;
          &lt;property name="password" value="myerp"/&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="applicationContextProvider"
            class="com.dummy.myerp.technical.util.spring.ApplicationContextProvider"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And in the test class, I loaded the context with <code>@ContextConfiguration</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>@ExtendWith(SpringExtension.class)
  @ContextConfiguration("classpath:transactionContextTest.xml")
  public class ComptabiliteManagerImplTest extends AbstractBusinessManager {
  
      private static ComptabiliteManagerImpl manager = new ComptabiliteManagerImpl();
  
      private static TransactionManager transactionManager;
  
      @Autowired
      public void setTransactionManager(TransactionManager transactionManager) {
          ComptabiliteManagerImplTest.transactionManager = transactionManager;
      }
  
      @BeforeAll
      private static void injectMockDao() {
          DaoProxy daoProxyMock = mock(DaoProxy.class, Mockito.RETURNS_DEEP_STUBS);
          AbstractBusinessManager.configure(null, daoProxyMock, transactionManager);
      }
  </code></pre>
  
  <p>The problem is: the TransactionManager is not loaded. When I run some tests that require it, I get a NullPointerException. </p>
  
  <p>What did I do wrong?</p>
  
  <p>EDIT : Instead of using @Autowired, I tried to retrieve the bean from the context like this : </p>
  
  <pre class="lang-java prettyprint-override"><code>@ExtendWith(SpringExtension.class)
  @ContextConfiguration("classpath:transactionContextTest.xml")
  public class ComptabiliteManagerImplTest extends AbstractBusinessManager {
  
      private static ComptabiliteManagerImpl manager = new ComptabiliteManagerImpl();
  
  
      private static ApplicationContext context = new ClassPathXmlApplicationContext("transactionContextTest.xml");
      private static TransactionManager transactionManager = (TransactionManager) context.getBean("transactionManager");
  
  
      @BeforeAll
      private static void injectMockDao() {
          DaoProxy daoProxyMock = mock(DaoProxy.class, Mockito.RETURNS_DEEP_STUBS);
          AbstractBusinessManager.configure(null, daoProxyMock, transactionManager);
      }
  </code></pre>
  
  <p>It works, but it's kind of a work arround and it makes clear that the issue is with the annotation @Autowired. Is there a clean and easy way to use @Autowired with JUnit5?</p>