title: How do I JUnit test a Spring autowired constructor?
body: <p>I have done quite a bit of searching online and I can't find an example of unit testing with an autowired constructor. I am using Spring to autowire in the values from a properties file to my application. I want to unit test MyApp.java's start method, but I have an autowired constructor so I don't know how to instantiate MyApp. Without the autowired properties, I was doing this in my unit test:</p>
  
  <pre><code>@Test
  public void testStart() {
    try{
     MyApp myApp = new MyApp();
     myApp.start();
     }
     catch (Exception e){
       fail("Error thrown")
     }
  }
  </code></pre>
  
  <p>I don't want to mock the autowiring, as I need to obtain the values from the properties file and to further complicate things, I am configuring everything through annotations. I don't have a spring.xml, application-context.xml, or a web.xml file. So how do I go about instantiating/testing MyApp's start method? I tried adding in @RunWith(SpringJUnit4ClassRunner.class) and autowiring MyApp myApp, but it throws errors about failing to load the application context that aren't fixed by implementing ApplicationContextAware on the test class.</p>
  
  <p>Here is MyApp.java</p>
  
  <pre><code>@Component
  public class MyApp {
  
      private static ApplicationContext applicationContext;
      private static MyAppProperties myAppProperties;
  
      //Obtain the values from the app.properties file
      @Autowired
      MyApp(MyAppProperties myAppProps){
          myAppProperties = myAppProps;
      }
  
      public static void main(String[] args) throws Exception {
       // Instantiate the application context for use by the other classes
       applicationContext = new AnnotationConfigApplicationContext("com.my.company");
  
       start();
      }
  
      /**
       * Start the Jetty server and configure the servlets
       * 
       * @throws Exception
       */
      public static void start() throws Exception {
          // Create Embedded Jetty server
          jettyServer = new Server();
  
          // Configure Jetty so that it stops at JVM shutdown phase
          jettyServer.setStopAtShutdown(true);
          jettyServer.setStopTimeout(7_000);
  
          // Create a list to hold all of the handlers
          final HandlerList handlerList = new HandlerList();
  
          // Configure for Http
          HttpConfiguration http_config = new HttpConfiguration();
          http_config.setSecureScheme("https");
          http_config.setSecurePort(myAppProperties.getHTTP_SECURE_PORT());
      ....
      }
  }
  </code></pre>
  
  <p>Here is my app.properties file</p>
  
  <pre><code># Spring Configuration for My application
  
  #properties for the embedded jetty server
  http_server_port=12345
  </code></pre>
  
  <p>Here is MyAppProperties.java</p>
  
  <pre><code>@Component
  public class MyAppProperties implements ApplicationContextAware {
  
      private ApplicationContext applicationContext;
  
      //List of values from the properties files to be autowired
      private int HTTP_SERVER_PORT;
      ...
  
      @Autowired
      public MyAppProperties( @Value("${http_server_port}") int http_server_port, ...){
          this.HTTP_SERVER_PORT = http_server_port;
      }
  
      /**
       * @return the applicationContext
       */
      public ApplicationContext getApplicationContext() {
          return applicationContext;
      }
  
      /**
       * @param applicationContext
       *            the applicationContext to set
       */
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) {
          this.applicationContext = applicationContext;
      }
  
      /**
       * @param name
       *            the name to set
       */
      public void setHTTP_SERVER_PORT(String name) {
          JETTY_SERVER_NAME = name;
      }
  
      /**
       * @return the httpServerPort
       */
      public int getHTTP_SERVER_PORT() {
          return HTTP_SERVER_PORT;
      }
  }
  </code></pre>
  
  <p>Here is MyAppTest.java</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  public class MyAppTest implements ApplicationContextAware{
  
      private ApplicationContext applicationContext;
  
      @Override
      public void setApplicationContext(ApplicationContext appContext) {
          applicationContext = appContext;    
      }
  
      @Autowired
      private MyApp myapp;
  
      @Test
      public void testStart(){
      try {
          if(myapp != null){
              myapp.start();
          }
          else{
              fail("myapp is null");
          }
      } catch (Exception e) {
          fail("Error thrown");
          e.printStackTrace();
      } 
      }
  }
  </code></pre>
  
  <p>UPDATE: Here is my configuration class</p>
  
  <pre><code>@Configuration
  @Component
  public class ApplicationConfig implements ApplicationContextAware {
  
      private final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfig.class);
      private ApplicationContext applicationContext;
  
      /**
       * @return the applicationContext
       */
      public ApplicationContext getApplicationContext() {
          LOGGER.debug("Getting Application Context", applicationContext);
          return applicationContext;
      }
  
      /**
       * @param applicationContext
       *            the applicationContext to set
       */
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) {
          this.applicationContext = applicationContext;
      }
  
      // Needed for @Value
      /**
       * Property sources placeholder configurer.
       *
       * @return the property sources placeholder configurer
       */
      @Bean
      public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer() {
          PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
          propertyPlaceholderConfigurer.setLocation(new ClassPathResource("app.properties"));
          return propertyPlaceholderConfigurer;
      }
      ...
  }
  </code></pre>