0

My question is: How can I test if EmailNotSendHandler is used when EmailNotSendException is thrown? or Second question: Whats the correct terminology to solve this Exception handling riddle?

**GOAL: Test to see if EmailNotSendHandler class is called when EmailNotSendException exception is thrown. WITH NO REST ANYTHING CONTROLLER **

Im fairly new to programming and do NOT KNOW the correct TERMINOLOGY. Im trying to learn pure Unit testing and exception handling with Spring, INTERNAL to the Spring Boot application. I've googled on how to test. Every search result boils down to: Write REST controllers so you can do REST calls to REST controller. I do not want to write REST controller for its out of scope of learning goals. Bassically anything that contains REST something is out of scope.

The @ControllerAdvice annotated class.

@ControllerAdvice
@Slf4j
public class EmailNotSendHandler {
    @ExceptionHandler(EmailNotSendException.class)
    private void processEmailNotSendExceptions(EmailNotSendException e){
        log.info("[QUATRO] Error handling? {}", e.getMessage());
    }
}

The exception class.

public class EmailNotSendException extends RuntimeException {
     public EmailNotSendException(String error) {
         super("test: " + error);
     }
}

The EmailService class

@Service
public class EmailService {
    private final JavaMailSender emailSenderContainer;

    @Value("email@email.email")
    private String receivingEmailAdress;

    public void sendSimpleMessage(String emailText) {
        SimpleMailMessage message = new SimpleMailMessage();

        if (emailTekst.isEmpty()) {
            return;
        }
        message.setTo(receivingEmailAdress);
        message.setSubject("Error");
        message.setText(emailText);
        emailSenderContainer.send(message);
    }
}

The Unit test

@Test()
public void testSimpleMailMessageSendFailed() {
    doThrow(new EmailNotSendException("")).when(emailServiceMocked).sendSimpleMessage("");
    emailServiceMocked.sendSimpleMessage("");
}
Joffrey Schmitz
  • 2,393
  • 3
  • 19
  • 28
Tovarisch
  • 1
  • 4
  • So, if I understood correctly, you want to test to see if the exception thrown by the service method is handled by the `@ControllerAdvice` class without sending a request to the controller that calls that service. Basically, Advices "wrap" the controllers and if any exception is thrown, they catch it and do whatever they are told to do. So, I do not think if it is possible to do that without using something like `MockMvc`. – Mansur Jun 16 '20 at 11:09
  • Well i cant call the REST controller for there is no rest controller. So basically, i want to test if processEmailNotSendExceptions method is run whenever EmailNotSendException is thrown – Tovarisch Jun 16 '20 at 11:21
  • So, what calls that service? Is it a scheduler? @EventListener or something else? For example if it is a scheduler, you can check [this](https://stackoverflow.com/a/53754698/9985287) out – Mansur Jun 16 '20 at 11:23
  • Yea a scheduler calls the email service method. Taking a look at link in your comment. Thanks you for helping Mensur! – Tovarisch Jun 16 '20 at 11:27

0 Answers0