我正在尝试为 Spring Email 编写 JUnit5 测试。当我运行测试时,Greenmail 服务器没有收到任何消息。附上相同的代码片段:
@ExtendWith(MockitoExtension.class)
public class EmailServiceImplTest {
Mail emailEntity;
GreenMail serverA;
@InjectMocks
private EmailServiceImpl emailService;
@Mock
private JavaMailSender javaMailSender;
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
emailEntity = mockItemMasterData();
// Setup fake smtp server.
serverA = new GreenMail(ServerSetupTest.SMTP);
serverA.start();
}
private Mail mockItemMasterData() {
Mail mail = new Mail();
mail.setFrom("no-reply@test.com");
mail.setTo("no-reply@test.com");
mail.setSubject("Spring Mail Integration Testing with JUnit and GreenMail Example");
mail.setBody("We show how to write Integration Tests using Spring and GreenMail.");
mail.setSendDate(Date.from(Instant.ofEpochSecond(System.currentTimeMillis())));
return mail;
}
@Test
public void testSendEmailToAvailableMailboxReturnEmail() throws SendFailedException {
// Using javamailsenderimpl to send the mail.
emailService.sendSimpleMessage(emailEntity);
try {
Message[] messages = serverA.getReceivedMessages();
Assert.assertNotNull(messages);
Assert.assertEquals(1, messages.length);
Assert.assertEquals(emailEntity.getSubject(), messages[0].getSubject());
Assert.assertEquals(emailEntity.getBody(), String.valueOf(messages[0].getContent()).contains("body"));
} catch (MessagingException | IOException e) {
Assert.fail("Should be able to retrive sent mail.");
}
}
}
以下是我正在为其编写此测试的课程
@Component
@Slf4j
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSender javaMailSender;
@Override
public void sendSimpleMessage(Mail mail) throws SendFailedException {
log.info("Send mail started ");
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(mail.getTo());
message.setFrom(mail.getFrom());
message.setSubject(mail.getSubject());
message.setText(mail.getBody());
message.setSentDate(mail.getSendDate());
javaMailSender.send(message);
log.info("Send mail end ");
}
}
以下是测试yaml源码:
---
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
我可以从EmailServiceImpl类发送电子邮件,但是当我尝试运行 test 时,它没有收到任何消息。
我尝试了一切,但没有得到任何运气。任何帮助将不胜感激。
谢谢。