【问题标题】:Disable database connections with @SpringBootTest使用 @SpringBootTest 禁用数据库连接
【发布时间】:2020-02-19 08:57:54
【问题描述】:

我有一个带有休眠功能的 SpringBoot 应用程序。在我的测试中,我想禁用任何类型的数据库连接和配置(测试无权访问数据库)。我该怎么做?

我的测试类用@SpringBootTest 注释并具有@Autowired 属性。 禁用所有数据库交互的正确注释值是什么?

@SpringBootTest
class MyTest {

  @Autowired
  ....
}

【问题讨论】:

    标签: hibernate jpa spring-boot-test


    【解决方案1】:

    您可以使用@TestPropertySource 覆盖application.properties 中的值。

    @TestPropertySource(locations="classpath:test.properties")
    

    A Quick Guide to @TestPropertySource

    【讨论】:

    • Spring 数据无论如何都会在 Spring 上下文中,与您的测试属性设置无关。
    • 这并不能解决问题,您的 db bean 仍在使用,它们与 db 没有连接。
    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 2020-10-10
    • 2021-08-22
    • 2019-05-09
    • 2018-11-10
    • 1970-01-01
    • 2011-12-29
    • 2020-06-28
    相关资源
    最近更新 更多