java - 尝试在 Spring Boot 测试属性配置上运行测试
问题描述
我有通常在 Postgres DB 上运行的 Spring Boot 应用程序,但是我想在嵌入式 H2 DB 上测试一些功能。我尝试使用注释我的测试类,@TestPropertySource
并尝试创建测试和开发配置文件(单独的 application.properties 文件),但这些似乎都不起作用。
这是我的应用程序-test.properties
server.port=8080
spring.resources.add-mappings=true
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.default_schema=public
这伴随着我的测试 data.sql 脚本:
insert into people(first_name, last_name, id) values('John', 'Johnson', 8);
insert into vehicles(name, make) values ('Mustang', 'Ford');
然而,即使我这样注释我的测试类:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@TestPropertySource(locations = {"classpath:application-test.properties"})
它仍然尝试使用常规 Postgres DB 运行应用程序,并且它使用的是 Postgres DB data.sql,而不是上面指定的那个。如果我将常规 data.sql 与测试切换,我会收到如下错误:
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
还有其他方法可以做到这一点吗?我能找到的解决方案都不适合我。
解决方案
推荐阅读
- shell - 用于打印单词和编号的 unix shell 脚本。以元音开头的单词
- reactjs - 如何使用 react-admin 构建分析仪表板?
- python - 即使在使用其他问题的答案后,列表也会更改另一个列表
- delphi - 列表框不显示对数据集所做的更改
- python - 为什么不可能从 Kivy 中的回调触发事件
- wordpress - 产品元数据作为 WooCommerce 中订单的订单项元数据
- node.js - 如何使用两个不同的用户对路线有不同的控制?
- r - 如何根据R中的虚拟值合并具有不相等行的两个数据集
- azerothcore - 需要帮助将我的旧版本 acore 转移到新版本
- r - ggplot2() 修复熔体变量的 x 轴