Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
忽略Spring测试中的MongoDB套接字连接_Spring_Mongodb_Testing_Spring Boot_Junit - Fatal编程技术网

忽略Spring测试中的MongoDB套接字连接

忽略Spring测试中的MongoDB套接字连接,spring,mongodb,testing,spring-boot,junit,Spring,Mongodb,Testing,Spring Boot,Junit,我正在spring项目中使用mongo,但我无法连接到mongo服务器。任何人都知道在执行测试时忽略这个bean的方法,因为有时候我没有启动mongo服务器,我不希望这个构建失败 我真的很想知道我是否可以用SpringRunner忽略它 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.spr

我正在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>

org.springframework.cloud
SpringCloudStarter父级
道尔斯顿释放
org.springframework.boot
spring启动程序数据mongodb

PS我故意在本地主机上停止了mongodb。

您可以通过向
应用程序测试添加以下注释来禁用Spring Boot对mongodb的自动配置:

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

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

我使用嵌入式mongodb解决了这个问题

依赖关系():

以及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();
    }
}

我将假设他的
应用程序。class
具有
@springbootplication
。另一种选择是在测试中使用嵌入式mongo依赖项:
de.flapdoodle.embedded de.flapdoodle.embed.mongo test
我正在使用嵌入式mongo测试这种方法。上面的答案回答了如何排除自动配置,但我的问题还没有解决。无法使用自动配置下载代理背后的嵌入式mongodb-在我的案例中,此答案无效您是否尝试在maven设置中添加代理配置?否,如何在mvn设置中添加代理配置?我不确定-我们可能不允许下载zip-out-side公司网络。
spring:
  data:
    mongodb:
      database: dbtest
      host: localhost
      port: 27028
@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();
    }
}