title: @Autowired failed under @SpringBootTest
body: <p>I am learning how to run @SpringBootTest the "right way", but is encountering a problem with autowiring in my test classes (in "src/test/java" directory):</p>
  
  <p>I have a class Graphs which is annotated by @Component in a package under "src/main/java":</p>
  
  <pre><code>@Component
  public class Graphs {
    ....
  }
  </code></pre>
  
  <p>Then, I created test classes under "src/test/java". One of them is:</p>
  
  <pre><code>@SpringBootTest
  public class GraphsTest {   
     @Test
     public void testRun () {
        Graphs graph = new Graphs(); // Using new to create an object
        if (graph==null) {
          System.out.println("It's null");
        } else {
          System.out.println("It's not null");
        }         
     }
  ...
  </code></pre>
  
  <p>When I test run "testRun" method, it produced "It's not null" as expected. </p>
  
  <p>After unit tests, i wanted to inject a "graph" as Graphs class was annotated by @Component thus a bean should be available for autowiring:</p>
  
  <pre><code>@SpringBootTest
  public class GraphTest {
     @Autowired
     private Graphs graph;    // auto inject a bean graph 
  
     @Test
     public void testRun () {
       if (graph==null) {
          System.out.println("it's null");
       } else {
          System.out.println("it's not null");
       }
     }
   ....
  </code></pre>
  
  <p>Now with autowiring, "testRun" always produced: "it's null" even when I attempted the following, ("xxxxxx" is the full name of the package which contains Graphs.java file):</p>
  
  <ol>
  <li>adding @Import(xxxxxx/Graphs.class) </li>
  <li>adding @ComponentScan("xxxxxxx") </li>
  <li>copying the Graphs.java file into the test package</li>
  <li><p>Adding a @Bean into TestConfiguration.java in the test package.</p>
  
  <p><code>@Bean
   public Graphs graph () {
      return new Graphs();
   }</code></p></li>
  </ol>
  
  <p>I started to suspect that I have fundamentally misunderstood/missed something about setting up a Spring Boot test environment: isn't it @SpringBootTest all I need to get started?</p>