'Getting Checked exception is invalid for this method! Invalid error in Mockito Unit tests
need ur help.
When I am writing a UT, Where my main function is something like this:
public String handleRequest(.......){
try {
.....
......
xyz.sendEmail(sesClient, emailSender, emailRecepient, mailSubject, mailBody);
.....
.....
catch (EmailMessagingException de) {
.......
}
}
public void sendEmail(@NonNull final AmazonSimpleEmailService sesClient, @NonNull String emailSender,
@NonNull String emailRecepient, @NonNull String subject, @NonNull String body) throws EmailMessagingException{
try {
SendEmailRequest request = new SendEmailRequest()
.withDestination(
new Destination().withToAddresses(emailRecepient)) .withMessage(new Message()
.withBody(new Body()
.withText(new Content()
.withCharset("UTF-8").withData(body)))
.withSubject(new Content()
.withCharset("UTF-8").withData(subject)))
.withSource(emailSender);
sesClient.sendEmail(request); } catch (Exception ex) { System.out.println("The email was not sent. Error message: "
ex.getMessage()); throw new EmailMessagingException("Error when sending email via ses", ex); } }
My UT looks like this:
public void testHandleRequestForEmailMessagingException()throws Exception{
when(sesClient.sendEmail(Mockito.any())).thenThrow(EmailMessagingException.class);
assertThrows(RuntimeException.class, ()->xyz.handleRequest(abc, context));
......
.....
verify(emailUtil).sendEmail(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
}
Now, when running this UT, I get error
Checked exception is invalid for this method! Invalid: EmailMessagingException I wrote another UT which tests similar other exception from a method call in same main method. I am not sure what I am doing wrong here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
