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("");
}