我在我的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的故意。
我要假設他的'Application.class'具有'@ SpringBootApplication'。 另一種選擇是使用測試嵌入式蒙戈依賴性: ' de.flapdoodle.embed de.flapdoodle.embed.mongo 測試 ' –
aorticDefiance
我triying這種方法,使用嵌入式mongo。上面的答案回答瞭如何排除自動配置,但我的問題尚未解決。 – bpedroso