使用 Java 测试邮件是否发送成功
Test if the mail sent sucessfully using Java
我有一个发送邮件的邮件服务:
@Component
public class MailingServiceImpl implements MailingService{
@Autowired
JavaMailSenderImpl mailSender;
@Override
public void sendMail(String sender, String receiver, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(sender);
message.setTo(receiver);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
我正在为此写一个测试void sendMail(...)
,我需要检查邮件是否发送成功。我发现 GreenMail
我目前正在尝试将其用于我的测试,代码如下:
public class MailingServiceImplTest {
private MailingServiceImpl mailingServiceImpl;
@Autowired
private JavaMailSenderImpl mailSender;
@PostConstruct
public void testConnection() {
try {
mailSender.testConnection();
} catch(MessagingException ex) {
throw new IllegalStateException("[Warning] Mail server is not available. " + ex);
}
}
@Rule
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
@Test
public void testSendMail() throws IOException {
GreenMailUtil.sendTextEmailTest(receiver, sender, subject, content);
// GreenMailUtil.sendTextEmailTest(to, from, subject, body); -- here I could use the GreenMailUtil sendTextEmailTest, but I need to use my void sendMail function
MimeMessage[] emails = greenMail.getReceivedMessages();
// assertEquals(1, emails.length);
}
有什么想法可以测试我的方法吗? GreenMail
(像下面的example 'Using JUint (rule based setup)' section) isn't the only tool I tried, tried Dumbster
(like example,但是sendMessage
需要字符串作为参数,我不能通过我的方法。我做错了吗?帮助
编辑
我对 JavaMailSenderImpl 的设置:
@Bean
public JavaMailSenderImpl mailSenderService(MailingServiceImpl mailingServiceImplementation,
@Value("${mail.host}") String host,
@Value("${mail.port}") int port,
@Value("${mail.username}") String username,
@Value("${mail.password}") String password) {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
最近在调试中发现 @Autowired MailingServiceImpl
是 null
..但是为什么呢?不是很懂
Greenmail 等正在启动一个可以从您的应用程序访问的本地 MailService。我还没有使用它,所以不要按原样使用代码,但从我的角度来看,您的测试应该看起来像……。像这样:
@Autowired
private MailingServiceImpl mailingServiceImpl;
@Rule
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
@Test
public void testSendMail() throws IOException {
mailingServiceImpl.sendMail("sender", "receiver", "subject", "content");
MimeMessage[] emails = greenMail.getReceivedMessages();
assertEquals(1, emails.length);
}
当然,您的 JavaMailSender 必须正确配置才能使用本地 GreenMail-SMTP 服务,当然您的 spring 测试设置必须正确(假设您要使用 spring) .
当使用 spring 启动时,这样的配置应该足够了:
spring.mail.host=localhost
spring.mail.port=3025 #Default port of greenmail?!
我有一个发送邮件的邮件服务:
@Component
public class MailingServiceImpl implements MailingService{
@Autowired
JavaMailSenderImpl mailSender;
@Override
public void sendMail(String sender, String receiver, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(sender);
message.setTo(receiver);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
我正在为此写一个测试void sendMail(...)
,我需要检查邮件是否发送成功。我发现 GreenMail
我目前正在尝试将其用于我的测试,代码如下:
public class MailingServiceImplTest {
private MailingServiceImpl mailingServiceImpl;
@Autowired
private JavaMailSenderImpl mailSender;
@PostConstruct
public void testConnection() {
try {
mailSender.testConnection();
} catch(MessagingException ex) {
throw new IllegalStateException("[Warning] Mail server is not available. " + ex);
}
}
@Rule
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
@Test
public void testSendMail() throws IOException {
GreenMailUtil.sendTextEmailTest(receiver, sender, subject, content);
// GreenMailUtil.sendTextEmailTest(to, from, subject, body); -- here I could use the GreenMailUtil sendTextEmailTest, but I need to use my void sendMail function
MimeMessage[] emails = greenMail.getReceivedMessages();
// assertEquals(1, emails.length);
}
有什么想法可以测试我的方法吗? GreenMail
(像下面的example 'Using JUint (rule based setup)' section) isn't the only tool I tried, tried Dumbster
(like example,但是sendMessage
需要字符串作为参数,我不能通过我的方法。我做错了吗?帮助
编辑
我对 JavaMailSenderImpl 的设置:
@Bean
public JavaMailSenderImpl mailSenderService(MailingServiceImpl mailingServiceImplementation,
@Value("${mail.host}") String host,
@Value("${mail.port}") int port,
@Value("${mail.username}") String username,
@Value("${mail.password}") String password) {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setPort(port);
mailSender.setUsername(username);
mailSender.setPassword(password);
return mailSender;
}
最近在调试中发现 @Autowired MailingServiceImpl
是 null
..但是为什么呢?不是很懂
Greenmail 等正在启动一个可以从您的应用程序访问的本地 MailService。我还没有使用它,所以不要按原样使用代码,但从我的角度来看,您的测试应该看起来像……。像这样:
@Autowired
private MailingServiceImpl mailingServiceImpl;
@Rule
public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
@Test
public void testSendMail() throws IOException {
mailingServiceImpl.sendMail("sender", "receiver", "subject", "content");
MimeMessage[] emails = greenMail.getReceivedMessages();
assertEquals(1, emails.length);
}
当然,您的 JavaMailSender 必须正确配置才能使用本地 GreenMail-SMTP 服务,当然您的 spring 测试设置必须正确(假设您要使用 spring) .
当使用 spring 启动时,这样的配置应该足够了:
spring.mail.host=localhost
spring.mail.port=3025 #Default port of greenmail?!