【问题标题】:Embedded MongoDB with Spring not working in test带有 Spring 的嵌入式 MongoDB 无法在测试中工作
【发布时间】:2021-12-05 16:40:35
【问题描述】:

我正在尝试使用嵌入式 MongoDB 创建一个测试类。 我无法开始测试,因为嵌入式 mongodb 似乎尚未启动。

我得到的异常如下:

2021-10-18 17:33:17 INFO  - [org.mongodb.driver.cluster:76] - Exception in monitor thread while connecting to server localhost:27019
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:272)
    at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:129)
    at java.base/sun.nio.ch.Invoker.invokeDirect(Invoker.java:158)
    at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:186)
    at java.base/sun.nio.ch.Invoker.invoke(Invoker.java:298)
    at java.base/sun.nio.ch.WindowsAsynchronousSocketChannelImpl$ConnectTask.failed(WindowsAsynchronousSocketChannelImpl.java:308)
    at java.base/sun.nio.ch.Iocp$EventHandlerTask.run(Iocp.java:389)
    at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.io.IOException: The remote computer refused the network connection.

嵌入式mongo的依赖:

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <scope>test</scope>
</dependency>

这是我的测试课

import ...

@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@EnableReactiveMongoRepositories
@ComponentScan(
    excludeFilters = {@ComponentScan.Filter(
            type = FilterType.ASSIGNABLE_TYPE,
            value = {MongoReactiveDataAutoConfiguration.class}
    )}
)
@Slf4j
public class MyServiceTest {

    @Autowired
    private MyService myService;


    @Test
    public void saveDataOnMongoTest(){
      
    }
}

这是我在测试配置文件下我的 yaml 上的 mongo 配置

  data:
    mongodb:
      uri: mongodb://localhost:27019/myDb
      database: myDb
      port: 27019  # embedded test purpose
      repositories:
        type: reactive
    jpa:
      repositories:
        enabled: true

  mongodb:
    embedded:
      version: 4.0.3
      features: sync_delay,no_http_interface_arg,only_with_ssl,only_windows_2008_server

【问题讨论】:

  • 尝试在您的测试配置文件中将 data.mongodb.port 设置为 0(零)。这种方式自动配置将使用动态映射的端口。
  • 尝试使用默认端口 - uri: mongodb://localhost:27017/myDb
  • @iska 我尝试将端口设置为 0,但没有帮助。
  • @mahbad 我检查了那个链接,但没有解决我的问题。

标签: java spring mongodb spring-boot flapdoodle-embed-mongo


【解决方案1】:

在主类中添加排除注释

@SpringBootTest @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})

它可能正在工作。

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 1970-01-01
    • 2020-09-05
    • 2016-08-26
    • 2018-07-23
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多