ReflectionTestUtils not working with @Autowired in Spring Test
<p>I am trying to add mock object in CourseServiceImpl's courseDao field but it is not working.</p>

<pre><code>@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
    locations = {"file:src/main/webapp/WEB-INF/config/servlet-config.xml"}
)
@ActiveProfiles("test")
public final class CourseServiceTest {
  @Mock
  private CourseDao courseDao;
  @Autowired
  private CourseService courseService;
  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
  }

@Test
  public void testCourseServiceNotNull() {
    assertNotNull(courseService);
    assertNotNull(courseDao);
    ReflectionTestUtils.setField(courseService, "courseDao", courseDao, CourseDao.class);
  }
</code></pre>

<p>The reflection statement throws an error that field "courseDao" didn't found. But, when I create an object using new operator then it works fine.</p>

<pre><code>ReflectionTestUtils.setField(new CourseServiceImpl(), "courseDao", courseDao, CourseDao.class);
</code></pre>

<p><strong>servlet-config.xml</strong></p>

<p></p>

<pre><code>&lt;mvc:annotation-driven /&gt;
&lt;mvc:resources location="pdfs" mapping="/pdfs/**" /&gt;

&lt;security:global-method-security
    pre-post-annotations="enabled" /&gt;

&lt;bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/view/" p:suffix=".jsp" p:order="2" /&gt;

&lt;bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
    p:contentNegotiationManager-ref="contentNegId" p:defaultViews-ref="defaultViewList"
    p:order="1" /&gt;

&lt;bean id="contentNegId"
    class="org.springframework.web.accept.ContentNegotiationManager"&gt;
    &lt;constructor-arg&gt;
        &lt;bean
            class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"&gt;
            &lt;constructor-arg&gt;
                &lt;map&gt;
                    &lt;entry key="json" value="application/json" /&gt;
                    &lt;entry key="xml" value="application/xml" /&gt;
                &lt;/map&gt;
            &lt;/constructor-arg&gt;
        &lt;/bean&gt;
    &lt;/constructor-arg&gt;
&lt;/bean&gt;

&lt;util:list id="defaultViewList"&gt;
    &lt;bean
        class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /&gt;
    &lt;bean class="org.springframework.web.servlet.view.xml.MarshallingView"&gt;
        &lt;constructor-arg&gt;
            &lt;bean class="org.springframework.oxm.xstream.XStreamMarshaller"
                p:autodetectAnnotations="true" /&gt;
        &lt;/constructor-arg&gt;
    &lt;/bean&gt;
&lt;/util:list&gt;

&lt;bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
    p:order="0" /&gt;

&lt;bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver"
    p:defaultLocale="en" /&gt;

&lt;bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource"
    p:basename="messages" /&gt;

&lt;context:property-placeholder location="classpath:messages.properties" /&gt;

&lt;import resource="/hibernate-config.xml" /&gt;
&lt;import resource="/hibernate-config-test.xml" /&gt;
</code></pre>

<p><strong>hibernate-config-test.xml</strong></p>

<pre><code>&lt;bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/school-repo-test" p:username="user"
        p:password="password" /&gt;


    &lt;bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        p:dataSource-ref="myDataSource" p:hibernateProperties-ref="hibernateProps"
        p:annotatedClasses-ref="mapClasses"&gt;
    &lt;/bean&gt;

    &lt;util:list id="mapClasses"&gt;
        &lt;value&gt;org.school.model.Course&lt;/value&gt;
        &lt;value&gt;org.school.model.Role&lt;/value&gt;
        &lt;value&gt;org.school.model.Staff&lt;/value&gt;
        &lt;value&gt;org.school.model.Student&lt;/value&gt;
        &lt;value&gt;org.school.model.Subject&lt;/value&gt;
    &lt;/util:list&gt;

    &lt;util:properties id="hibernateProps"&gt;
        &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
        &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
        &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
    &lt;/util:properties&gt;

    &lt;bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" /&gt;
</code></pre>

<p>Apart from profile name, rest is same for hibernate-config file.</p>

<p><strong>CourseServiceImpl</strong></p>

<pre><code>@Service(value = "courseService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public class CourseServiceImpl implements CourseService {
  @Autowired
  private MessageSource messageSource;
  @Autowired
  private CourseDao courseDao;
  @Autowired
  private ApplicationContext context;
</code></pre>

<p>Please advise.</p>