title: @SpringBootTest vs @ContextConfiguration vs @Import in Spring Boot Unit Test
body: <p>I'm working on a Spring Boot project.
  I'm writing a <code>Unit Test</code> code based on <code>TDD</code> which is a little bit difficult.</p>
  <p><code>@SpringBootTest</code> loaded all beans, which led to longer test times.</p>
  <p>So I used the <code>@SpringBootTest</code>'s class designation.</p>
  <p>I completed the test normally, but I am not sure the difference between using <code>@ContextConfiguration</code> and using <code>@Import</code>.</p>
  <p>All three options run normally. I want to know which choice is the best.</p>
  <pre class="lang-java prettyprint-override"><code>@Service
  public class CoffeeService {
  
      private final CoffeeRepository coffeeRepository;
  
      public CoffeeService(CoffeeRepository coffeeRepository) {
          this.coffeeRepository = coffeeRepository;
      }
  
      public String getCoffee(String name){
          return coffeeRepository.findByName(name);
      }
  }
  
  public interface CoffeeRepository {
      String findByName(String name);
  }
  
  @Repository
  public class SimpleCoffeeRepository implements CoffeeRepository {
  
      @Override
      public String findByName(String name) {
          return &quot;mocha&quot;;
      }
  }
  
  Option 1 (SpringBootTest Annotation) - OK  
  @RunWith(SpringRunner.class)
  @SpringBootTest(classes = {CoffeeService.class, SimpleCoffeeRepository.class})
  public class CoffeeServiceTest {
  
      @Autowired
      private CoffeeService coffeeService;
  
      @Test
      public void getCoffeeTest() {
          String value = coffeeService.getCoffee(&quot;mocha&quot;);
          assertEquals(&quot;mocha&quot;, value);
      }
  }
  
  
  Option 2 (ContextConfiguration Annotation) - OK
  @RunWith(SpringRunner.class)
  @ContextConfiguration(classes = {SimpleCoffeeRepository.class, CoffeeService.class})
  public class CoffeeServiceTest {
  
      @Autowired
      private CoffeeService coffeeService;
  
      @Test
      public void getCoffeeTest() {
          String value = coffeeService.getCoffee(&quot;mocha&quot;);
          assertEquals(&quot;mocha&quot;, value);
      }
  }
  
  Option 3 (Import Annotation) - OK
  @RunWith(SpringRunner.class)
  @Import({SimpleCoffeeRepository.class, CoffeeService.class})
  public class CoffeeServiceTest {
  
      @Autowired
      private CoffeeService coffeeService;
  
      @Test
      public void getCoffeeTest() {
          String value = coffeeService.getCoffee(&quot;mocha&quot;);
          assertEquals(&quot;mocha&quot;, value);
      }
  </code></pre>