【问题标题】:Ignore MongoDB socket connection on Spring Test在 Spring Test 上忽略 MongoDB 套接字连接
【发布时间】:2018-01-17 13:01:33
【问题描述】:

我在我的 spring 项目中使用 mongo,但我无法连接到 mongo 服务器。任何人都知道在执行测试时忽略此 bean 的方法,因为有时我没有启动 mongo 服务器并且我不希望此构建失败。

我真的很想知道我是否可以使用 SpringRunner 忽略它。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class })
public class ApplicationTests {
    @Test
    public void contextLoads() {
    }
}

堆栈跟踪:

Caused by: org.springframework.dao.DataAccessResourceFailureException: 
Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. 
Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: localhost}, caused by {java.net.UnknownHostException: localhost}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: localhost}, caused by {java.net.UnknownHostException: localhost}}]

弹簧组件:

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>Dalston.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

PS 我故意在 localhost 停止了 mongodb。

【问题讨论】:

    标签: spring mongodb testing spring-boot junit


    【解决方案1】:

    您可以通过在 ApplicationTests 类中添加以下注释来禁用 Spring Boot 的 MongoDB 自动配置:

    @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
    

    这将阻止 Spring Boot 创建 MongoClient(假设您的测试上下文中没有 other 类用 @EnableAutoConfiguration@SpringBootApplication 注释)。

    【讨论】:

    • 我假设他的Application.class@SpringBootApplication。另一种选择是使用嵌入式 mongo 依赖项进行测试:&lt;dependency&gt; &lt;groupId&gt;de.flapdoodle.embed&lt;/groupId&gt; &lt;artifactId&gt;de.flapdoodle.embed.mongo&lt;/artifactId&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt;
    • 我正在尝试这种方法,使用嵌入式 mongo。上面的答案回答了如何排除自动配置,但是我的问题还没有解决。
    【解决方案2】:

    我使用嵌入式 mongodb 解决了。

    依赖(https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo):

    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <scope>test</scope>
    </dependency>
    

    我在 application-test.yml 上添加了特定配置(使用 spring.profile.active=test 执行)

    spring:
      data:
        mongodb:
          database: dbtest
          host: localhost
          port: 27028
    

    还有 ApplicationTests.java

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = { Application.class })
    public class ApplicationTests {
    
        private static final String LOCALHOST = "127.0.0.1";
        private static final String DB_NAME = "dbtest";
        private static final int MONGO_TEST_PORT = 27028;
        private static MongodProcess mongoProcess;
        private static Mongo mongo;
    
        @BeforeClass
        public static void initializeDB() throws IOException {
            MongodStarter starter = MongodStarter.getDefaultInstance();
            IMongodConfig mongodConfig = new MongodConfigBuilder()
                .version(Version.Main.V3_3)
                .net(new Net(LOCALHOST, MONGO_TEST_PORT, Network.localhostIsIPv6()))
                .build();
    
            MongodExecutable mongodExecutable = null;
            try {
                mongodExecutable = starter.prepare(mongodConfig);
                mongoProcess = mongodExecutable.start();
                mongo = new MongoClient(LOCALHOST, MONGO_TEST_PORT);
                mongo.getDB(DB_NAME);
            } finally {
                if (mongodExecutable != null)
                    mongodExecutable.stop();
            }
        }
    
        @Test
        public void contextLoads() {}
    
        @AfterClass
        public static void shutdownDB() throws InterruptedException {
            if (mongo != null) mongo.close();
            if (mongoProcess != null) mongoProcess.stop();
        }
    }
    

    【讨论】:

    • stackoverflow.com/q/50579963/297907 unable-to-download-embedded-mongodb-behind-proxy-using-automatic-configuration - 在我的情况下这个答案没有用
    • 您是否尝试在您的 Maven 设置中添加代理配置?
    • 否,如何在 mvn 设置中添加代理配置?我不确定 - 我们可能不允许下载 zip out side 公司网络。
    猜你喜欢
    • 2019-02-28
    • 2021-01-02
    • 2013-05-15
    • 2018-09-15
    • 2013-05-24
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多