MailSenderAdapter { void sendMessage(TemplateMessage message) { var addressee = message.getAddressee(); var addresseeMail = userService.getUserData(addressee).getEmail(); var mailMessage = new SimpleMailMessage(); mailMessage.setSubject(message.getSubject()); mailMessage.setText(message.getText()); mailMessage.setFrom(properties.getFromMail()); mailMessage.setTo(addresseeMail); mailSender.send(mailMessage); log.info(String.format("Mail sent to %s!", addresseeMail)); } MailSenderAdapter(MessageRepository messageRepository,
                             JavaMailSender mailSender,
                             UserService userService,
                             MailConfigurationProperties properties,
                             Clock clock); @Scheduled(fixedDelay = 10000L) @Transactional void sendPendingMessages(); @Transactional T queueMessage(T message);  }
@Test public void sendMessage() { var expectedFrom = "sender@test.com"; var expectedTo = "mail@googlemail.com"; var expectedSubject = "This is a subject :-)"; var expectedText = "This is a cool text and very long too!"; var expectedSimpleMessage = new SimpleMailMessage(); expectedSimpleMessage.setFrom(expectedFrom); expectedSimpleMessage.setTo(expectedTo); expectedSimpleMessage.setSubject(expectedSubject); expectedSimpleMessage.setText(expectedText); given(templateMessageMock.getSubject()).willReturn(expectedSubject); given(templateMessageMock.getText()).willReturn(expectedText); given(templateMessageMock.getAddressee()).willReturn(userMock); given(userServiceMock.getUserData(userMock)).willReturn(userDataMock); given(userDataMock.getEmail()).willReturn(expectedTo); given(mailPropertiesMock.getFromMail()).willReturn(expectedFrom); mailSenderAdapter.sendMessage(templateMessageMock); verify(javaMailSenderMock).send(expectedSimpleMessage); }