【发布时间】: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