title: Springboot test failed to load ApplicationContext in Junit 5
body: <p>I'm trying to create unit/integration test using Junit5 for specific service classes to avoid overload the whole project.</p>
  
  <p>So here I try to run the <code>EmailService</code> with its dependency classes inside, but I got <code>java.lang.IllegalStateException: Failed to load ApplicationContext. Error creating bean with name 'emailSenderService. No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate.'</code>.</p>
  
  <p>Do I must have to run the whole application to test a single service?</p>
  
  <p>build.yml</p>
  
  <pre><code>{
      testImplementation ('org.springframework.boot:spring-boot-starter-test') {
          exclude group: 'junit', module: 'junit'
      }
      testImplementation "org.junit.jupiter:junit-jupiter:5.4.1"
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>public class EmailSenderService {
      private final JavaMailSender sender;
      private final SpringTemplateEngine templateEngine;
      private final MessageSource i18n;
      public EmailSenderService(JavaMailSender sender, SpringTemplateEngine templateEngine,
                                @Qualifier("messageSource") MessageSource i18n) {
          this.sender = sender;
          this.templateEngine = templateEngine;
          this.i18n = i18n;
      }
  }
  </code></pre>
  
  <p>test class:</p>
  
  <pre><code>@SpringBootTest(
          classes = {EmailSenderService.class}
  )
  @ExtendWith({SpringExtension.class})
  class EmailServiceTest {
  
      private static GreenMail smtp;
  
      @Autowired
      private EmailSenderService mailService;
  
      @BeforeAll
      static void init() {
          smtp = new GreenMail(new ServerSetup(3026,null,"smtp"));
          smtp.start();
      }
  
      @AfterAll
      static void tearDown() {
          smtp.stop();
      }
  
      @BeforeEach
      void clearUp() throws FolderException {
          smtp.purgeEmailFromAllMailboxes();
      }
  
      @Test
      void testNewBidRequestEmail() throws MessagingException {
          EmailMessageTemplateDto contact = new EmailMessageTemplateDto("test","test@test.com","test message");
          mailService.sendUserContactEmail(contact);
          Assertions.assertTrue(smtp.waitForIncomingEmail(1));
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>2019-04-03 14:56:06.146  WARN 732 --- [           main]
    o.s.w.c.s.GenericWebApplicationContext   : Exception encountered
    during context initialization - cancelling refresh attempt:
    org.springframework.beans.factory.UnsatisfiedDependencyException:
    Error creating bean with name 'emailSenderService': Unsatisfied
    dependency expressed through constructor parameter 0; nested exception
    is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    qualifying bean of type
    'org.springframework.mail.javamail.JavaMailSender' available: expected
    at least 1 bean which qualifies as autowire candidate. Dependency
    annotations: {} 2019-04-03 14:56:06.153 ERROR 732 --- [<br>
    main] o.s.b.d.LoggingFailureAnalysisReporter   : </p>
    
    <p>*************************** APPLICATION FAILED TO START</p>
    
    <hr>
    
    <p>Description:</p>
    
    <p>Parameter 0 of constructor in
    com.server.server.service.EmailSenderService required a bean of
    type 'org.springframework.mail.javamail.JavaMailSender' that could not
    be found.</p>
    
    <p>Action:</p>
    
    <p>Consider defining a bean of type
    'org.springframework.mail.javamail.JavaMailSender' in your
    configuration.</p>
    
    <p>2019-04-03 14:56:06.159 ERROR 732 --- [           main]
    o.s.test.context.TestContextManager      : Caught exception while
    allowing TestExecutionListener
    [org.springframework.test.context.web.ServletTestExecutionListener@342c38f8]
    to prepare test instance
    [com.server.server.test.junit.EmailServiceTest@4c7a078]</p>
    
    <p>java.lang.IllegalStateException: Failed to load ApplicationContext    at
    org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    ~[spring-test-5.1.5.RELEASE.jar:5.1.5.RELEASE]    at
    org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    ...</p>
  </blockquote>