带有通用容器的com.mongodb.MongoSocketOpenException

时间:2019-10-16 14:58:01

标签: java mongodb docker

我想使用Java对象“ GenericContainer”测试我的代码,但是有以下异常:

  

com.mongodb.MongoSocketOpenException:异常打开套接字       在com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70)       在com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128)       在com.mongodb.internal.connection.DefaultServerMonitor $ ServerMonitorRunnable.run(DefaultServerMonitor.java:131)       在java.base / java.lang.Thread.run(Thread.java:834)   引起原因:java.net.ConnectException:连接被拒绝(连接被拒绝)       在java.base / java.net.PlainSocketImpl.socketConnect(本机方法)       在java.base / java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)       在java.base / java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)       在java.base / java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)       在java.base / java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)       在java.base / java.net.Socket.connect(Socket.java:591)       在com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64)       在com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79)       在com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)       ...省略了3个常见框架

我的代码:

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import java.time.Duration;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

@Slf4j
public abstract class AbstractContainerBaseTest {
    protected static final String MONGO_DB_NAME = "db_name";

    private final int port = 27017;

    private final GenericContainer mongo = new GenericContainer<>("mongo:4.0.8").withExposedPorts(port).withStartupTimeout(Duration.ofSeconds(1180L));

    protected MongoClient mongoClient;

    protected void init() {
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        int maxConnectionIdleTime = 60000;
        builder.maxConnectionIdleTime(maxConnectionIdleTime);

        mongo.start();
        mongo.waitingFor(Wait.forListeningPort()
             .withStartupTimeout(Duration.ofSeconds(180L)));

        ServerAddress serverAddress = new ServerAddress(mongo.getContainerIpAddress(), mongo.getMappedPort(port));

        mongoClient = new MongoClient(serverAddress, builder.build());
    }

    @AfterEach
    protected void close() {
        mongo.stop();
    }
}

以及GenericContainer的maven依赖项:

    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>1.11.3</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>javax.annotation</groupId>
                <artifactId>javax.annotation-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

有人可以帮助我吗?

谢谢你。

0 个答案:

没有答案