2017-03-11 41 views
0

我已經使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎以及程序包生成了一個Spring Boot Web應用程序作爲可執行JAR文件。使用在SpringBoot中測試電子郵件服務

技術:

春季啓動1.4.2.RELEASE,春天4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat的嵌入8.5.6時,Maven 3,Java的8

我有我想測試

@Service 
public class MailClient { 

    protected static final Logger looger = LoggerFactory.getLogger(MailClient.class); 

    @Autowired 
    private JavaMailSender mailSender; 

    private MailContentBuilder mailContentBuilder; 

    @Autowired 
    public MailClient(JavaMailSender mailSender, MailContentBuilder mailContentBuilder) { 
     this.mailSender = mailSender; 
     this.mailContentBuilder = mailContentBuilder; 
    } 

    //TODO: in a properties 
    public void prepareAndSend(String recipient, String message) { 
     MimeMessagePreparator messagePreparator = mimeMessage -> { 
      MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage); 
      messageHelper.setFrom("[email protected]"); 
      messageHelper.setTo(recipient); 
      messageHelper.setSubject("Sample mail subject"); 
      String content = mailContentBuilder.build(message); 
      messageHelper.setText(content, true); 
     }; 
     try { 
      if (looger.isDebugEnabled()) { 
       looger.debug("sending email to " + recipient); 
      } 
      mailSender.send(messagePreparator); 
     } catch (MailException e) { 
      looger.error(e.getMessage()); 
     } 
    } 
} 

這封電子郵件服務,我創建了這個測試類

@RunWith(SpringRunner.class) 
public class MailClientTest { 

    @Autowired 
    private MailClient mailClient; 

    private GreenMail smtpServer; 

    @Before 
    public void setUp() throws Exception { 
     smtpServer = new GreenMail(new ServerSetup(25, null, "smtp")); 
     smtpServer.start(); 
    } 

    @Test 
    public void shouldSendMail() throws Exception { 
     //given 
     String recipient = "[email protected]"; 
     String message = "Test message content"; 
     //when 
     mailClient.prepareAndSend(recipient, message); 
     //then 
     String content = "<span>" + message + "</span>"; 
     assertReceivedMessageContains(content); 
    } 

    private void assertReceivedMessageContains(String expected) throws IOException, MessagingException { 
     MimeMessage[] receivedMessages = smtpServer.getReceivedMessages(); 
     assertEquals(1, receivedMessages.length); 
     String content = (String) receivedMessages[0].getContent(); 
     System.out.println(content); 
     assertTrue(content.contains(expected)); 
    } 

    @After 
    public void tearDown() throws Exception { 
     smtpServer.stop(); 
    } 

} 

但我去t運行測試時發生此錯誤

Error creating bean with name 'com.tdk.service.MailClientTest': Unsatisfied dependency expressed through field 'mailClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.service.MailClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
+0

爲我們正在運行引導應用程序的Mail()方法提供main()方法的包結構。 –

+1

作爲一面評論。只要有可能,不要重新發明輪子。只要看看** [Spring Boot Email Tools library](https://github.com/ozimov/spring-boot-email-tools)** – JeanValjean

回答

0

問題是您要運行集成測試而不提供bean! 無論何時使用@Autowired,都需要通過上下文提供自動佈線組件饋送所需的bean。 因此,您需要在@Configuration批註中添加該測試類中的靜態類。此外,測試類還需要知道通過@ContextConfiguration註釋必須使用哪個配置。這裏有一個例子:

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes = {MailClientTest.ContextConfiguration.class}) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class MailClientTest { 

    @Configuration 
    @TestPropertySource(locations = "classpath:application.properties", 
      properties ={"my.additiona.property=123"}) 
    @ComponentScan(basePackages = {"com.tdk.service"}) 
    public static class ContextConfiguration { 
     @Bean 
     public JavaMailSender mailSender{ 
     return ... //create the instance here 
     } 

     @Bean  
     public MailContentBuilder mailContentBuilder() { 
     return ... //create the instance here 
     } 
    } 
} 

無論如何,我已經在評論中指出,不要浪費你的時間重新發明輪子。那裏已經有一個圖書館爲你做這些事情。我在談論Spring Boot Email Tools。 我認爲這是值得使用的圖書館,也許有助於與新功能的存儲庫,而不是花時間重新實現模板引擎的電子郵件支持。