简体   繁体   English

使用 @DataJpaTest 的测试未使用嵌入式内存数据库

[英]Tests with @DataJpaTest are not using embedded in-memory database

As per the DataJpaTest documentation tests annotated with @DataJpaTest will by default use embedded in-memory database(h2)根据DataJpaTest 文档,使用@DataJpaTest注释的测试将默认使用嵌入式内存数据库(h2)

By default, tests annotated with @DataJpaTest are transactional and roll back at the end of each test.默认情况下,使用 @DataJpaTest 注释的测试是事务性的,并在每个测试结束时回滚。 They also use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource).他们还使用嵌入式内存数据库(替换任何显式或通常自动配置的数据源)。 The @AutoConfigureTestDatabase annotation can be used to override these settings. @AutoConfigureTestDatabase 注释可用于覆盖这些设置。

But in my case it is not happening, Test class annotated with @DataJpaTest is loading the postgres dialect instead of using h2 dialect.但在我的情况下,它没有发生,用@DataJpaTest 注释的测试@DataJpaTest正在加载 postgres 方言而不是使用 h2 方言。

Below is the property file from main/resources下面是来自 main/resources 的属性文件

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres?currentSchema=test
    username: test
    password: test
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect

Whats happening发生了什么

I found that spring is not picking dialect from application.yml file from main/resources instead of honouring @DataJpaTest and using h2 dialect.我发现 spring 没有从 main/resources 的application.yml文件中选择方言,而不是尊重@DataJpaTest并使用 h2 方言。

Solution解决方案

While trying different variations with jpa properties, I found that this issue is happening only when database-platform property is present in the application properties file.在尝试使用 jpa 属性的不同变体时,我发现仅当应用程序属性文件中存在database-platform属性时才会发生此问题。 If this property is removed then its loading the h2 dialect for test cases and postgres dialect of main application.如果此属性被删除,那么它会加载测试用例的 h2 方言和主应用程序的 postgres 方言。

Why?为什么?

This seems to be a bug in the spring codebase, since it is nowhere documented that one needs to remove database-platform property for @DataJpaTest to use in-memory database.这似乎是 spring 代码库中的一个错误,因为没有记录表明需要删除database-platform属性以使@DataJpaTest使用内存数据库。

Also there is one more confusion around database-platform property, as per this documentation根据本文档,关于database-platform属性还有一个更混乱的地方

spring.jpa.database-platform spring.jpa.database-platform

Name of the target database to operate on, auto-detected by default.要操作的目标数据库的名称,默认自动检测。 Can be alternatively set using the "Database" enum.也可以使用“数据库”枚举进行设置。

Seems like one more bug in the documentation, as the spring.jpa.database-platform only accepts dialect class as a value and no other enum value will work.似乎是文档中的另一个错误,因为spring.jpa.database-platform仅接受方言 class 作为值,没有其他枚举值将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 内存数据库测试未创建表 - In-memory database tests not creating tables 如何使用 Spring Boot 针对真实数据库而不是在内存中一次性运行 @DataJpaTest - How to one-off run @DataJpaTest against real database instead of in-memory with Spring Boot 使用Apache Derby的内存嵌入式服务器 - In-memory embedded server using Apache Derby 如何使用H2嵌入式数据库创建H2内存数据库 - How to create H2 in-memory database using H2 embedded database Jersey e2e使用Spring的JDBCTemplate对内存数据库进行集成测试 - Jersey e2e integration tests for in-memory database using Spring's JDBCTemplate 配置内存或嵌入式数据库以测试生产代码 - Configuring in-memory or embedded database to test production code 用于集成测试的内存数据库的架构创建失败 - Schema creation for in-memory database to use in integration tests fails 如何进行集成测试setUp()操作在嵌入式Jetty容器中运行的内存数据库中的HSQL数据? - How to have integration tests setUp() manipulate HSQL data in an in-memory DB running in embedded Jetty container? 使用内存数据库在 Spring Boot 中进行集成测试 - Integration test in Spring Boot using in-memory database 使用Java类在内存数据库中添加数据? - add data in in-memory database using Java Classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM