小编Nor*_*adX的帖子

Java Testcontainers - 无法连接到公开端口

我使用 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
...
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

java junit docker dockerfile testcontainers

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

docker ×1

dockerfile ×1

java ×1

junit ×1

testcontainers ×1