Reputation: 275
I want to make unit test of sending message by using it.ozimov.springboot.mail.service.EmailService
.
https://github.com/ozimov/spring-boot-email-tools
Here is my MailService:
import com.google.common.collect.Lists;
import it.ozimov.springboot.mail.model.Email;
import it.ozimov.springboot.mail.model.defaultimpl.DefaultEmail;
import it.ozimov.springboot.mail.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.io.UnsupportedEncodingException;
@Service
public class MailService {
@Autowired
public EmailService emailService;
@Value("${spring.mail.username}")
private String username;
public void sendMail(String subject, String messageContent, String recipient)
throws UnsupportedEncodingException, AddressException {
final Email email = DefaultEmail.builder()
.from(new InternetAddress(username))
.to(Lists.newArrayList(new InternetAddress(recipient)))
.subject(subject)
.body(messageContent)
.encoding("UTF-8").build();
emailService.send(email);
}
}
And I used GreenMail to make a test, but it doesn't work:
@RunWith(SpringJUnit4ClassRunner.class)
public class MailServiceTest {
private static final String USER_PASSWORD = "abcdef123";
private static final String USER_NAME = "hascode";
private static final String EMAIL_USER_ADDRESS = "hascode@localhost";
private static final String EMAIL_TO = "[email protected]";
private static final String EMAIL_SUBJECT = "Test E-Mail";
private static final String EMAIL_TEXT = "This is a test e-mail.";
private static final String LOCALHOST = "127.0.0.1";
private GreenMail mailServer;
@InjectMocks
private MailService mailService = new MailService();
@Mock
private EmailService emailService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mailServer = new GreenMail(ServerSetupTest.SMTP);
mailServer.start();
mailServer.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);
}
@After
public void tearDown() {
mailServer.stop();
}
@Test
public void getMails() throws IOException, MessagingException,
UserException, InterruptedException {
String subject = "Some subject";
String body = "Some contents.";
String recipient = "[email protected]";
ReflectionTestUtils.setField(mailService, "username", "bla");
mailService.sendMail(subject, body, recipient);
MimeMessage[] messages = mailServer.getReceivedMessages();
assertNotNull(messages);
assertEquals(1, messages.length);
MimeMessage m = messages[0];
assertEquals(EMAIL_SUBJECT, m.getSubject());
assertTrue(String.valueOf(m.getContent()).contains(EMAIL_TEXT));
assertEquals(EMAIL_TO, m.getFrom()[0].toString());
}
}
As the result I got this:
java.lang.AssertionError:
Expected :1
Actual :0
Somebody have maybe any idea what I'm doing wrong? Because MailService works in controller (it sends email), but test doesn't work.
Upvotes: 0
Views: 324
Reputation: 17713
The problem is that you used unit testing with mocked EmailService to be used with GreenMail that aims to support integration testing.
If you look into the used library you will see the difference. E.g. look into this context-based test
Upvotes: 0