I am facing an issue with testing my email service. When I run test it fails due to Null Pointer Exeption generated on List<EmailModel> receivedMessages = server.mailBox();
But my emails are recieved on fakesmtp server mailbox. Could you direct me how to handle this case?
Test class
@SpringBootTest
@ActiveProfiles("email")
@RunWith(SpringRunner.class)
class EmailServiceTest
{
@Autowired
EmailService emailService;
@Rule
public FakeSmtpRule server = new FakeSmtpRule(ServerConfiguration.create().port(25).bind("localhost"));
@Test
void sendEmail() throws MessagingException, IOException
{
// Given
String to = "test@test.com";
String title = "Title";
String content = "Content";
// When
emailService.sendEmail(to, title, content);
// Then
List<EmailModel> receivedMessages = server.mailBox();
assertTrue(receivedMessages.size() == 1);
EmailModel receivedMessage = receivedMessages.get(0);
assertTrue(receivedMessage.getTo().equals(to));
assertTrue(receivedMessage.getSubject().equals(title));
assertTrue(receivedMessage.getEmailStr().equals(content));
}
}
EmailService class
@Service
public class EmailService
{
private JavaMailSender javaMailSender;
@Autowired
public EmailService(JavaMailSender javaMailSender)
{
this.javaMailSender = javaMailSender;
}
/*Set email attributes and send it via javaMailSender*/
public void sendEmail(String receiverEmail, String subject, String message)
{
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(receiverEmail);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(message);
javaMailSender.send(simpleMailMessage);
}
}
Aucun commentaire:
Enregistrer un commentaire