如何在Spring Boot中模拟db连接以进行测试?

如何在Spring Boot中模拟db连接以进行测试?,第1张

如何在Spring Boot中模拟db连接以进行测试?

有一个选项可以伪造仅具有纯Spring功能的Spring
bean。您需要使用

@Primary
@Profile
@ActiveProfiles
为它的注解

我写了一篇有关该主题的博客文章。

您可以使用内存DB(例如H2)替换实际数据源。像这样:

@Configurationpublic class TestingDataSourceConfig {    @Bean    @Primary    public DataSource dataSource() {        return new EmbeddedDatabaseBuilder() .generateUniqueName(true) .setType(H2) .setscriptEncoding("UTF-8") .ignoreFailedDrops(true) .addscript("schema.sql") .addscripts("user_data.sql", "country_data.sql") .build();    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5675877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存