@Service
public class EmailService {
private JavaMailSenderImpl mailSender;
@PostConstruct
protected void init() {
mailSender= new JavaMailSenderImpl();
mailSender.setHost("some.server.com");
mailSender.setPort("25");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
}
public void sendEmail(String recipient, String subject, String message){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom("someone@somewhere.com");
simpleMailMessage.setTo(recipient);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(message);
mailSender.send(simpleMailMessage);
}
}
Above code is to send emails.
public class EmailServiceTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@InjectMocks
private EmailService emailService;
@Mock
private JavaMailSenderImpl mailSender;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testEmail(){
exception.expect(MailSendException.class);
ReflectionTestUtils.setField(emailService, "mailSender", mailSender);
emailService.sendEmail("me.com", "test", "this is a unit test");
}
}
I have written a test case where I expect the mail send to fail, however I am not getting any errors In EmailServicetest testEmail() I expect a MailSendException as the server doesnt exist. Can anyone tell me what might be the reason?
Aucun commentaire:
Enregistrer un commentaire