首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Greenmail进行Springboot测试

通过Greenmail进行Springboot测试
EN

Stack Overflow用户
提问于 2020-05-15 13:27:51
回答 1查看 2.4K关注 0票数 1

我试着在Spring中测试我的Gmail,并遵循了这个教程。我已经按照本教程中的指定实现了它,但是我的EmailService抛出一个MailSendException:

代码语言:javascript
复制
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 2525; timeout 5000;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 2525; timeout 5000;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
; message exception details (1) are:
Failed message 1:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 2525; timeout 5000;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2210)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722)
    at javax.mail.Service.connect(Service.java:342)

有人知道怎么解决这个问题吗?(从未测试过诸如SMTP/Email之类的内容,因此只需遵循上面的教程。编辑:我可以发送电子邮件没有任何问题手动,但我需要测试。我的application.yml:

代码语言:javascript
复制
spring.mail.host: smtp.gmail.com
spring.mail.port: 587
spring.mail.properties.mail.smtp.starttls.enable: true
spring.mail.username: ************@gmail.com
spring.mail.password: ***************
spring.mail.properties.mail.smtp.starttls.required: true
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.connectiontimeout: 5000
spring.mail.properties.mail.smtp.timeout: 5000
spring.mail.properties.mail.smtp.writetimeout: 5000
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-01 13:15:41

从日志中可以看到,Spring试图连接到端口2525,但无法连接。这意味着在测试执行期间,这个端口上没有正在运行的邮件服务器--这应该由JUnit规则实现提供(如果您正在使用JUnit5使用扩展)

确保您的测试配置已正确设置。这意味着,检查您的测试代码。

代码语言:javascript
复制
@Rule
public SmtpServerRule smtpServerRule = new SmtpServerRule(2525);

火柴

src/test/resources/application.yml

代码语言:javascript
复制
spring:
  mail:
    default-encoding: UTF-8
    host: localhost
    jndi-name:
    username: username
    password: secret
    port: 2525
    properties:
      mail:
        debug: false
        smtp:
          debug: false
          auth: true
          starttls: true
    protocol: smtp
    test-connection: false

我还建议更新来自教程的代码并添加测试用户--因为它可以与安全协议一起使用。

代码语言:javascript
复制
public class SmtpServerRule extends ExternalResource {
// omitted for brevity
@Override
protected void before() throws Throwable {
   super.before();
   smtpServer = new GreenMail(new ServerSetup(port, null, "smtp"));
   smtpServer.addUser("username", "secret");
   smtpServer.start();
}
// omitted for brevity
}

另外,更具体的设置请参考官方的GreenMail文档

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61820439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档