title: Not able to unit test the exception thrown in this method using mockito
body: <p>I have written the following unit test using <code>mockito</code> to test my <code>EmailService.java</code> class. What I am not sure is if I am testing this correctly or not (both happy path and exception scenario).</p>
  
  <p>Moreover I am getting this <code>Error: 'void' type not allowed here</code>
  in the following code snippet in my unit test </p>
  
  <pre><code>when(mockEmailService.notify(anyString())).thenThrow(MailException.class);
  </code></pre>
  
  <p>I understand that since my <code>notify()</code> method is returning void I am getting that exception. But not sure how to resolve this. Is there any code change required in my unit test or actual class or both?</p>
  
  <p>Can somebody please guide.</p>
  
  <p><strong>EmailServiceTest.java</strong></p>
  
  <pre><code>public class EmailServiceTest {
  
      @Rule
      public MockitoJUnitRule rule = new MockitoJUnitRule(this);
  
      @Mock
      private MailSender mailSender;
      @Mock
      private EmailService mockEmailService;
      private String emailRecipientAddress = "recipient@abc.com";
      private String emailSenderAddress = "sender@abc.com";
      private String messageBody = "Hello Message Body!!!";
  
      @Test
      public void testNotify() {
          EmailService emailService = new EmailService(mailSender, emailRecipientAddress, emailSenderAddress);
          emailService.notify(messageBody);
      }
  
      @Test(expected = MailException.class)
      public void testNotifyMailException() {
          when(mockEmailService.notify(anyString())).thenThrow(MailException.class);
          EmailService emailService = new EmailService(mailSender, emailRecipientAddress, emailSenderAddress);
          emailService.notify(messageBody);
      }
  
  }
  </code></pre>
  
  <p><strong>EmailService.java</strong></p>
  
  <pre><code>public class EmailService {
      private static final Log LOG = LogFactory.getLog(EmailService.class);
      private static final String EMAIL_SUBJECT = ":: Risk Assessment Job Summary Results::";
      private final MailSender mailSender;
      private final String emailRecipientAddress;
      private final String emailSenderAddress;
  
      public EmailService(MailSender mailSender, String emailRecipientAddress,
              String emailSenderAddress) {
          this.mailSender = mailSender;
          this.emailRecipientAddress = emailRecipientAddress;
          this.emailSenderAddress = emailSenderAddress;
      }
  
      public void notify(String messageBody) {
          SimpleMailMessage message = new SimpleMailMessage();
          message.setSubject(EMAIL_SUBJECT);
          message.setTo(emailRecipientAddress);
          message.setFrom(emailSenderAddress);
          message.setText(messageBody);
          try {
              mailSender.send(message);
          } catch (MailException e) {
              LOG.error("Error while sending notification email: ", e);
          }
      }
  }
  </code></pre>