Reputation: 139
I have a class JavaMailDao that i want to test using JUnit and mockito. I want to test the Catch part
try {
this.mailSender.send(msg);
} catch(MailException ex) {
throw new BackendException(DaoExceptionType.EMAIL_ERROR);
}
In the test i have this:
Mockito.doNothing().doThrow(new MailException()).when(this.mailSenderMock).send(Mockito.any(SimpleMailMessage.class));
The problem it's it says : "can't instantiate the type MailException", and i have the import too by the way.
mport org.springframework.mail.MailException;
Does anyone know how to do it? Thank you!
Upvotes: 0
Views: 1750
Reputation: 545
MailException
is an abstract class. Abstract classes cannot be instantiated, so instantiate one of it's subclasses, such as MailSendException
.
Solution:
Mockito.doNothing().doThrow(new MailSendException("Test message")).when(this.mailSenderMock).send(Mockito.any(SimpleMailMessage.class));`
See the Spring Framework docs for more info and suitable subclasses.
Upvotes: 4