title: Spring + TestNG integration tests, injecting DAO with annotations fails
body: <p>I first did not mention what was the key component of this issue: I am using TestNG here.</p>
  
  <p>I have a DAO layer performing persistence. It works fine as part of my little web app (I have a classic Controller, Service, DAO layers design). I can update this question with my XMLs if required.</p>
  
  <p>My Service layer</p>
  
  <pre><code>@Service
  public class UserServiceImpl implements UserService {
  
      @Autowired
      private UserDao userDao;
  
      @Override
      public GoodVibeUserDetails getUser(String username) throws UsernameNotFoundException {
  
          GoodVibeUserDetails user = userDao.getDetailsRolesAndImagesForUser(username);
  
          return user;
      }
  
      // more methods...
  
  }
  </code></pre>
  
  <p>My DAO layer</p>
  
  <pre><code>@Repository
  public class UserDaoImplHibernate implements UserDao {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      // My methods using sessionFactory &amp; "talking" to the Db via the sessionFactory
  }
  </code></pre>
  
  <p>And here is my Test Class</p>
  
  <pre><code>@Component
  public class UserDaoImplHibernateTests{
  
      @Autowired
      private UserDao userDao;
  
      private GoodVibeUserDetails user; 
  
      @BeforeMethod
      public void beforeEachMethod() throws ParseException{
          user = new GoodVibeUserDetails();
          user.setUsername("adrien");
          user.setActive(true);
          // &amp; so on...
      }
  
      /*
       * When everything is fine - test cases
       */
      @Test
      public void shouldAcceptRegistrationAndReturnUserWithId() throws Exception{
          assertNotNull(userDao) ;
          user = userDao.registerUser(user);
          assertNotNull(user.getId()) ;
      }
  
      // more test cases...
  
  }
  </code></pre>
  
  <p>But for my test class the Autowiring, <code>userDao</code> always returns <strong>Null</strong>, I'm only starting to do tests in Spring and I'm a bit lost. Any pointers are welcome.</p>
  
  <hr>
  
  <p>Latest edit after Boris Treukhov's answer</p>
  
  <pre><code>import ...
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.testng.annotations.BeforeMethod;
  import org.testng.annotations.Test;
  import static org.junit.Assert.assertNotNull;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("/applicationContext.xml")
  public class UserDaoImplHibernateTests{
  
      @Autowired
      @Qualifier("userDao")
      private UserDao userDao;
  
      private GoodVibeUserDetails user; 
  
      @BeforeMethod
      public void beforeEachMethod() throws ParseException{
          user = new GoodVibeUserDetails();
          user.setUsername("adrien");
          user.setActive(true);
          // &amp; so on...
      }
  
      /*
       * When everything is fine - test cases
       */
      @Test
      public void shouldAcceptRegistrationAndReturnUserWithId() throws Exception{
          assertNotNull(userDao) ;
          user = userDao.registerUser(user);
          assertNotNull(user.getId()) ;
      }
  
      // more test methods...
  
  }
  </code></pre>
  
  <p>And this is my <strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:tx="http://www.springframework.org/schema/tx" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:p="http://www.springframework.org/schema/p" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" &gt;
  
  
      &lt;!-- the application context definition scans within the base package of the application --&gt;
      &lt;!-- for @Components, @Controller, @Service, @Configuration, etc. --&gt;
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.goodvibes" /&gt;
  
      &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" /&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
          p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" 
          p:username="${jdbc.username}" p:password="${jdbc.password}" /&gt;
  
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="configurationClass"&gt;
              &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${jdbc.show_sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.SetBigStringTryClob"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;0&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      [...]
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I did not add a repository-config.xml as this should be enough to access <code>userDao</code>. I still get userDao equal null though.</p>
  
  <p>Thanks in advance</p>