如何在Java中创建模拟电子邮件服务?

huangapple go评论72阅读模式
英文:

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>

SMTPServerjames-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: &#39;org.apache.james&#39;, name: &#39;james-server-protocols-smtp&#39;, version: &#39;3.5.0&#39;
implementation group: &#39;org.apache.james&#39;, name: &#39;metrics-api&#39;, version: &#39;3.5.0&#39;
implementation group: &#39;org.apache.james&#39;, name: &#39;metrics-logger&#39;, version: &#39;3.5.0&#39;
implementation group: &#39;org.apache.james.protocols&#39;, name: &#39;protocols-netty&#39;, version: &#39;3.5.0&#39;

For Maven:

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.james&lt;/groupId&gt;
        &lt;artifactId&gt;james-server-protocols-smtp&lt;/artifactId&gt;
        &lt;version&gt;3.5.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.james.protocols&lt;/groupId&gt;
        &lt;artifactId&gt;protocols-netty&lt;/artifactId&gt;
        &lt;version&gt;3.5.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.james&lt;/groupId&gt;
        &lt;artifactId&gt;metrics-api&lt;/artifactId&gt;
        &lt;version&gt;3.5.0&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.james&lt;/groupId&gt;
        &lt;artifactId&gt;metrics-logger&lt;/artifactId&gt;
        &lt;version&gt;3.5.0&lt;/version&gt;
    &lt;/dependency&gt;

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

huangapple
  • 本文由 发表于 2020年9月18日 16:42:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63952274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定