【发布时间】:2021-06-13 03:21:55
【问题描述】:
我正在使用 Flapdoodle's Embedded Mongo 在我的 Spring Boot 应用程序上运行集成测试。
我有如下测试:
@SpringBootTest(classes = Application.class)
@TestMethodOrder(OrderAnnotation.class)
class IntegrationTests {
@BeforeAll
static void setup() throws Exception {
String ip = "localhost";
int port = 65000;
IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
.net(new Net(ip, port, Network.localhostIsIPv6())).build();
MongodStarter starter = MongodStarter.getDefaultInstance();
mongodExecutable = starter.prepare(mongodConfig);
mongodExecutable.start();
mongoTemplate = new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, ip, port)), "test");
}
但是每次MongoDB服务器在一个随机端口上启动:
[2021-03-16T01:41:26.026Z] [com.mongodb.diagnostics.logging.SLF4JLogger] [main] [71] [INFO ] Opened connection [connectionId{localValue:4, serverValue:2}] to localhost:55359
我已尝试为端口使用不同的值,但它们都不起作用。
为什么我的配置没有得到尊重?
如果我需要任何其他信息,请告诉我。
【问题讨论】:
-
CONNECTION_STRING 中有什么?
-
@PhilippeSimo
mongodb://localhost:55172 -
我的意思是 CONNECTION_STRING 的初始值:模板。当您使用 string.format 时,模板是什么
-
@PhilippeSimo 初始值为
mongodb://%s:%d -
所以你应该得到
mongodb://localhost:65000而不是你提到的mongodb://localhost:55172。我会尝试重现您的问题并让您知道
标签: java spring mongodb spring-boot spring-mongodb