title: Autowiring not working in Spring 3.1.2, JUnit 4.10.0
body: <p>Using Spring 3.1.2, JUnit 4.10.0, and pretty new to both versions. I'm having the problem that I can't get the annotation-based autowiring to work.</p>
  
  <p>Below are two samples, the one not using annotations, which is working fine. And the second one using annotation, which doesn't work, and I don't find the reason. I followed the samples of spring-mvc-test pretty much.</p>
  
  <p><strong>Working</strong>:</p>
  
  <pre><code>package com.company.web.api;
  // imports
  
  public class ApiTests {   
  
      @Test
      public void testApiGetUserById() throws Exception {
          ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/company/web/api/ApiTests-context.xml");
          UserManagementService userManagementService = (UserManagementService) ctx.getBean("userManagementService");
          ApiUserManagementController apiUserManagementController = new ApiUserManagementController(userManagementService);
          MockMvc mockMvc = standaloneSetup(apiUserManagementController).build();
  
          // The actual test     
          mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
      }
  }
  </code></pre>
  
  <p><strong>Failing</strong>, because <code>userManagementService</code> is null, not getting autowired:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration       // should default to ApiTests-context.xml in same package
  public class ApiTests {
  
      @Autowired
      UserManagementService userManagementService;
  
      private MockMvc mockMvc;
  
      @Before
      public void setup(){
          // SetUp never gets called?!
      }
  
      @Test
      public void testGetUserById() throws Exception {
  
          // !!! at this point, userManagementService is still null - why? !!!       
  
          ApiUserManagementController apiUserManagementController 
              = new ApiUserManagementController(userManagementService);
  
          mockMvc = standaloneSetup(apiUserManagementController).build();
  
          // The actual test
          mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
      }
  }
  </code></pre>
  
  <p>Note that both test classes above should be using the same context configuration, and the userManagementService is defined in there.</p>
  
  <p>ApiTests-context.xml:</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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;amp;characterEncoding=utf8"/&gt;
          &lt;property name="username" value="user"/&gt;
          &lt;property name="password" value="passwd"/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Hibernate SessionFactory --&gt;
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
            p:dataSource-ref="dataSource" p:mappingResources="company.hbm.xml"&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.generate_statistics"&gt;${hibernate.generate_statistics}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
          &lt;property name="eventListeners"&gt;
              &lt;map&gt;
                  &lt;entry key="merge"&gt;
                      &lt;bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/&gt;
                  &lt;/entry&gt;
              &lt;/map&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
            p:sessionFactory-ref="sessionFactory"/&gt;
  
      &lt;!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= --&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;tx:annotation-driven/&gt;
      &lt;context:mbean-export/&gt;
  
      &lt;!-- tried both this and context:component-scan --&gt;
      &lt;!--&lt;bean id="userManagementService" class="com.company.web.hibernate.UserManagementServiceImpl"/&gt;--&gt;
      &lt;context:component-scan base-package="com.company"/&gt;
  
      &lt;!-- Hibernate's JMX statistics service --&gt;
      &lt;bean name="application:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService" autowire="byName"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>and the UserManagementService (interface) as well as UserManagementServiceImpl has the <code>@Service</code> annotation.</p>
  
  <p>Two minor questions/observations: setup() never gets called, even though it has the @Before annotation. Furthermore, I noticed that my test methods don't get executed/recognized if they don't start with the name 'test', which is not the case though with all spring-mvc-test samples I saw.</p>
  
  <p>pom.xml:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.junit&lt;/groupId&gt;
          &lt;artifactId&gt;com.springsource.org.junit&lt;/artifactId&gt;
          &lt;version&gt;4.10.0&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/uma4p.png" alt="enter image description here"></p>
  
  <p><strong>Update:</strong></p>
  
  <p>The problem only occurs when I run the tests from maven; it's ok when I run the test from within my IDE (IntelliJ IDEA).</p>
  
  <pre><code>        &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.12.3&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;includes&gt;
                      &lt;include&gt;**/*Tests.java&lt;/include&gt;
                  &lt;/includes&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
  </code></pre>