https://github.com/bvfalcon/junit5-fakesmtp
FakeSMTP for unit-testing smtp clients with JUnit 5
https://github.com/bvfalcon/junit5-fakesmtp
junit junit5 maven smtp smtp-server testing unit-testing
Last synced: 6 months ago
JSON representation
FakeSMTP for unit-testing smtp clients with JUnit 5
- Host: GitHub
- URL: https://github.com/bvfalcon/junit5-fakesmtp
- Owner: bvfalcon
- License: apache-2.0
- Created: 2022-08-02T09:29:26.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T17:30:53.000Z (over 3 years ago)
- Last Synced: 2025-07-04T08:11:40.222Z (12 months ago)
- Topics: junit, junit5, maven, smtp, smtp-server, testing, unit-testing
- Language: Java
- Homepage: https://bvfalcon.github.io/junit5-fakesmtp/
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# JUnit5-FakeSMTP
[](https://maven-badges.herokuapp.com/maven-central/name.bychkov/junit5-fakesmtp)
FakeSMTP for unit-testing smtp clients with JUnit 5
# Unit-testing with fake smtp-server
## Problem description
Suppose, your application sends emails to users with such or similar code:
```java
public class SendEmailService {
public void sendMessage(String email, String subject, String body) throws MessagingException {
Properties props = System.getProperties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props, null);
Message simpleMail = new MimeMessage(session);
simpleMail.setSubject(subject);
simpleMail.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
MimeMultipart mailContent = new MimeMultipart();
MimeBodyPart mailMessage = new MimeBodyPart();
mailMessage.setContent(body, "text/html; charset=utf-8");
mailContent.addBodyPart(mailMessage);
simpleMail.setContent(mailContent);
Transport.send(simpleMail);
}
}
```
You must be sure this functionality will work correct in future and not break, while the code changes. How can you do this? Every time you can start simple smpt-server locally. After tests runned, see messages and prove it. For small projects this is only uncomfortable, for large - impossible.
## Solution
These actions can be performed automatically. Use in code of your unit-test special extension and smtp-server will start and stop automatically:
```java
@RegisterExtension
static FakeSmtpJUnitExtension fakeSmtp = new FakeSmtpJUnitExtension();
@Test
public void testSendMessage() {
String expectedReceiver = "test-email-" + new Random().nextInt(Integer.MAX_VALUE) + "@example.com";
String expectedSubject = "test-subject-" + new Random().nextInt(Integer.MAX_VALUE);
try {
SendEmailService testedService = new SendEmailService();
testedService.sendMessage(expectedReceiver, expectedSubject, "text of body");
Assertions.assertEquals(1, fakeSmtp.getMessages().size());
MimeMessage actualMail = fakeSmtp.getMessages().iterator().next();
Assertions.assertEquals(expectedReceiver, actualMail.getAllRecipients()[0].toString());
Assertions.assertEquals(expectedSubject, actualMail.getSubject());
} catch (MessagingException e) {
Assertions.fail(e);
}
}
```
# Minimum requirements
- Java 8
- Maven 3.2.5
- JUnit 5.1
# Using in your project
Add in your pom.xml these modifications
```xml
...
...
name.bychkov
junit5-fakesmtp
1.0-SNAPSHOT
test
...
maven-surefire-plugin
2.22.0
```
Notes:
1) maven-surefire-plugin must have version >= 2.22.0
## JavaMail and Jakarta Mail
By default, implementation uses JavaMail realization (namespaces `javax.mail.`). If you use Jakarta Mail (namespaces `jakarta.mail.`), use dependency with classifier `jakarta`:
```xml
name.bychkov
junit5-fakesmtp
1.0-SNAPSHOT
jakarta
test
```
## More samples
You can see full examples of usage JUnit5-FakeSMTP with [JavaMail](./src/it/javamail/) and [Jakarta Mail](./src/it/jakartamail/).