英文:
How to create mock email service in Java?
问题
我正在编写一个小型的测试库,其中包括不同的模拟服务,如HTTP、SFTP、存储桶和电子邮件。
在处理电子邮件模拟服务时,我遇到了一些困惑。我找到了Apace James文档和一篇文章,但我不知道如何将其适配到我的接口中,而且使用SMTP服务器让我感到很困惑。
interface TestServer {
void start();
void stop();
}
我的想法是创建一个实现,将整个设置放在构造函数中,我需要在设置和拆卸阶段仅启动和停止模拟。
如何使用Apache James服务来实现这一点呢?
我使用Java 11、Spring Boot和JUnit 5。
英文:
I am writing a small testing library with different mock services such as HTTP, SFTP, Buckets, Email.
I have a mind block when it comes to Email Mock service. I find a Apace James docs and an article but I don't see how I can adapt it to my interface and it is confusing to work with SMTP Servers.
interface TestServer {
void start();
void stop();
}
The idea is to create an implementation, so the whole setup will be in the constructor and I need to start and stop Mock only in set up and tear down phases.
How can I do it with the Apache James service?
I use Java 11, Spring Boot, and JUnit 5.
答案1
得分: 1
你可以通过使用 org.apache.james.smtpserver.netty.SMTPServer
来实现这个。为了做到这一点,你需要一些依赖项。
对于 Gradle:
implementation group: 'org.apache.james', name: 'james-server-protocols-smtp', version: '3.5.0'
implementation group: 'org.apache.james', name: 'metrics-api', version: '3.5.0'
implementation group: 'org.apache.james', name: 'metrics-logger', version: '3.5.0'
implementation group: 'org.apache.james.protocols', name: 'protocols-netty', version: '3.5.0'
对于 Maven:
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>james-server-protocols-smtp</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.james.protocols</groupId>
<artifactId>protocols-netty</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>metrics-api</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>metrics-logger</artifactId>
<version>3.5.0</version>
</dependency>
SMTPServer
是 james-server-protocols-smtp
的一部分,但其他依赖项用于度量。
你的接口的一个示例实现可能如下所示:
public class MySmtpServer implements TestServer {
private final SMTPServer smtpServer;
public MySmtpServer(final int port) {
MetricFactory metricFactory = new DefaultMetricFactory();
SmtpMetrics smtpMetrics = new SmtpMetricsImpl(metricFactory);
SMTPServer smtpServer = new SMTPServer(smtpMetrics);
smtpServer.setListenAddresses(new InetSocketAddress(port));
this.smtpServer = smtpServer;
}
@Override
public void start() {
smtpServer.start();
}
@Override
public void stop() {
smtpServer.stop();
}
}
英文:
You can achieve this by using org.apache.james.smtpserver.netty.SMTPServer
. To do this you will need some dependencies.
For Gradle :
implementation group: 'org.apache.james', name: 'james-server-protocols-smtp', version: '3.5.0'
implementation group: 'org.apache.james', name: 'metrics-api', version: '3.5.0'
implementation group: 'org.apache.james', name: 'metrics-logger', version: '3.5.0'
implementation group: 'org.apache.james.protocols', name: 'protocols-netty', version: '3.5.0'
For Maven:
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>james-server-protocols-smtp</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.james.protocols</groupId>
<artifactId>protocols-netty</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>metrics-api</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>metrics-logger</artifactId>
<version>3.5.0</version>
</dependency>
SMTPServer
is part of james-server-protocols-smtp
but others are needed for metrics.
A sample implementation of your interface can look like this :
public class MySmtpServer implements TestServer {
private final SMTPServer smtpServer;
public MySmtpServer(final int port) {
MetricFactory metricFactory = new DefaultMetricFactory();
SmtpMetrics smtpMetrics = new SmtpMetricsImpl(metricFactory);
SMTPServer smtpServer = new SMTPServer(smtpMetrics);
smtpServer.setListenAddresses(new InetSocketAddress(port));
this.smtpServer = smtpServer;
}
@Override
public void start() {
smtpServer.start();
}
@Override
public void stop() {
smtpServer.stop();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论