我使用 javax.mail 实现了 POP3 服务器和客户端,只是为了尝试使用 Docker 进行集成测试。因此,我基于 openjdk:8-jre 映像创建了两个 Docker 映像,并将我的 jar 复制到其中并启动它。根据我的配置(见下文),它正在工作。他们正在互相交谈。
但是,由于想要进行多个集成测试,为每个测试构建一个映像并启动它们将是一件很乏味的事情。我也不知道如何自动化结果。但后来我偶然发现了 TestContainers,这似乎对实施这些测试有很大帮助。
因此,我开始使用 POP3 服务器映像作为 GenericContainer 将这些测试移植到 TestContainers,并在 JUnit 测试方法中启动我的 POP3 客户端类。我公开了 POP3 服务器正在侦听的端口 24999。但是当我尝试连接到服务器时,出现以下错误:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 32782; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused
...
TestContainers 中可能缺少一些设置。请你帮助我好吗。
这是我正在使用的代码:
public class DockerPop3AutocryptKeyProvidingAndReceivingTest {
@Test
public void test() throws InterruptedException {
GenericContainer container = new GenericContainer<>("immerfroehlich/emailfilter:latest")
.withExposedPorts(24999);
container.start();
String host = container.getContainerIpAddress();
String port = container.getFirstMappedPort().toString();
//The following is simplified, but copied from the working jar used in the Docker Client image/container
MyPOP3Client client = new MyPOP3Client(host, port);
client.connect();
container.stop();
}
}
这就是我创建 Docker 镜像的方式:
FROM openjdk:8-jre
ADD build/distributions/MyPOP3Server.tar . #This is where I have packed all the needed files to. It gets unpacked by Docker.
#EXPOSE 24999 #I tried both with and without this expose
WORKDIR /MyPOP3Server/bin
ENTRYPOINT ["sh","MyPOP3Server"] #Executes the shell script which runs java with my jar
这是在 Server Jar 内运行的代码的简化版本:
MyPOP3Server server = new MyPOP3Server();
server.listenToPort(24999);
请告诉我我错过了什么。这里有什么问题吗?
谢谢并致以亲切的问候。
RISEBY
慕妹3242003
ABOUTYOU
慕容3067478
相关分类