title: NullPointerException in Spring Test Class
body: <p>I am getting a NullPointerException while testing Spring Junit test configurations. The issue is with using the @ContextConfiguration and the @Autowired annotation.</p>
  
  <p>When I instantiate the context and get a reference to the bean directly, as shown with the commented out code in the test method, the test runs correctly and succeeds. But when I try to use the @ContextConfiguration and @autowired annotations, with the same XML file attribute I get a NullPointerException in my assertEquals statement. Do you have any idea what I'm doing wrong?</p>
  
  <pre><code>package com.greathouse.helloworld.HelloWorldTest;
  
  import javax.inject.Inject;
  
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import junit.framework.TestCase;
  
  @ContextConfiguration(locations = {"classpath:/resources/application-config.xml"})
  @RunWith(SpringJUnit4ClassRunner.class)
  public class AppTest extends TestCase
  {
      @Autowired
      MessageService messageService;
  
      @Test
      public void testApp()
      {
          //ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/resources/application-config.xml");
          //messageService = context.getBean("printMessage", MessageService.class);
          assertEquals( messageService.getMessage(), "Hello World" );
      }
  }
  </code></pre>