我使用javax.mail实现了一个POP3服务器和客户端,只是为了尝试使用Docker进行集成测试。因此,我基于openjdk:8-jre图像创建了两个Docker镜像,并将我的jars文件复制到它们中并启动了它们。根据我的配置(见下文),一切都正常。它们彼此通信。
但是,由于我想要进行多个集成测试,为每个测试构建一个镜像并启动它们将变得很繁琐。而且我不知道如何自动化结果。
但是,我偶然发现了TestContainers,似乎这会在实现这些测试时提供很大的帮助。
因此,我开始将这些测试移植到TestContainers中,使用我的POP3服务器映像作为GenericContainer,并在JUnit测试方法中启动我的POP3客户端类。我公开了端口24999,我的POP3服务器正在监听该端口。但是当我尝试连接服务器时,我遇到了以下错误:
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
这是运行在服务器Jar内的代码的简化版本:
MyPOP3Server server = new MyPOP3Server();
server.listenToPort(24999);
请告诉我错在哪里,我错过了什么?这儿有什么问题吗?
感谢您的关注和祝愿。