title: Spring Boot Unit Test Autowired
body: <p>I have the following classes :</p>
  
  <p><strong>ApplicationAndConfiguration class</strong> </p>
  
  <pre><code>package mypackage.service;
  
  import mypackage.service.util.MyUtility;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.Bean;
  
  @SpringBootApplication
  public class ApplicationAndConfiguration {
  
  
      public static void main(String[] args) {
          ApplicationContext context = SpringApplication.run(ApplicationAndConfiguration.class, new String[]{});
      }
  
      @Bean(initMethod="init")
      public MyUtility birtUtil() {
          return new MyUtility();
      }
  }
  </code></pre>
  
  <p><strong>MyRestController class</strong></p>
  
  <pre><code>package mypackage.service.controllers;
  
  import mypackage.service.util.MyUtility;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class MyRestController {
  
      @Autowired
      private MyUtility util;
  
      @RequestMapping("/getLibraryName")
      public String getMessageFromRest(@RequestParam String name) {
  
          return "name was " + name + "//" + util.getMessage();
      }   
  }
  </code></pre>
  
  <p><strong>MyUtility class</strong></p>
  
  <pre><code>package mypackage.service.util;
  
  public class MyUtility {
  
      private String message;
  
      public void init() {
          setMessage("MyUtility correctly initialized!");
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  }
  </code></pre>
  
  <p>When I start the application and run it as an independant jar, or from the IDE (Eclipse), no problem at all, everything works as expected.</p>
  
  <p>However, I want to write a unit test to test my <em>MyRestController</em> class ... and I'm getting a NPE because the Autowired field util is null (within <em>MyRestController</em> class).</p>
  
  <p>Here is my test class :</p>
  
  <pre><code>package mypackage.service.controllers;
  
  import static org.hamcrest.Matchers.equalTo;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  import mypackage.service.ApplicationAndConfiguration;
  
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.boot.test.SpringApplicationConfiguration;
  import org.springframework.http.MediaType;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  
  @SpringApplicationConfiguration(classes = ApplicationAndConfiguration.class)
  @WebAppConfiguration
  @RunWith(SpringJUnit4ClassRunner.class)
  public class TestController {
  
      private MockMvc mvc;
  
      @Before
      public void setup() throws Exception {
          mvc = MockMvcBuilders.standaloneSetup(new MyRestController()).build();
      }
  
      @Test
      public void MyTestController() throws Exception {
  
          mvc.perform(MockMvcRequestBuilders.get("/getLibraryName").param("name", "test").accept(MediaType.APPLICATION_JSON))
          .andExpect(status().isOk())
          .andExpect(content().string(equalTo("name was test//MyUtility correctly initialized!")));
      }
  }
  </code></pre>
  
  <p>I'm definitely missing something so that my Autowired field gets filled during tests, and not only during standard application execution ...</p>
  
  <p>Any pointer why it doesn't work ?</p>