2017-08-09 90 views
1

我在我的spring項目中使用了mongo,但無法連接到mongo服務器。任何人都知道在執行測試時忽略這個bean的方法,因爲有時我沒有mongo服務器,我不希望這個構建失敗。忽略春季測試中的MongoDB套接字連接

我真的很想知道是否可以使用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}}] 

Spring組件:

<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我在本地主機停止MongoDB的故意。

回答

0

我解決了使用嵌入式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> 

我添加上應用test.yml特定配置(執行使用spring.profile.active =試驗)

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

您可以通過添加以下標註您ApplicationTests級傷殘的MongoDB的春天啓動的自動配置:

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

這將防止彈簧引導從創建MongoClient(假設沒有其他類在你的測試環境註釋爲@EnableAutoConfiguration@SpringBootApplication)。

+1

我要假設他的'Application.class'具有'@ SpringBootApplication'。 另一種選擇是使用測試嵌入式蒙戈依賴性: ' de.flapdoodle.embed de.flapdoodle.embed.mongo 測試 ' – aorticDefiance

+0

我triying這種方法,使用嵌入式mongo。上面的答案回答瞭如何排除自動配置,但我的問題尚未解決。 – bpedroso