【问题标题】:How to prevent spring JPA to create DB schema when running tests运行测试时如何防止spring JPA创建数据库模式
【发布时间】:2021-04-15 07:23:23
【问题描述】:

我有一个中等大小的 Spring Boot 应用程序。当我在 IntelliJ 中运行测试时,一切正常。

我的 spring 应用程序从 testdata.sql 文件中的 CommandLineRunner 加载一组静态测试数据。该文件创建数据库模式并加载准备好的数据。这非常快并且工作正常。这样我的测试总是针对同一组数据运行。 (测试夹具。)

但是当我用 maven 运行我的测试时,它失败了。第一个测试运行得很好。但是第二次测试失败了。似乎 spring-jpa 试图自动生成模式。虽然我在application-test.yml 中停用了它

来自 maven 运行的错误消息和相关日志

DEBUG .(StartupInfoLogger.java:56).logStarting()                             | Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
INFO  .(SpringApplication.java:655).logStartupProfileInfo()                  | The following profiles are active: test
INFO  .(RepositoryConfigurationDelegate.java:127).registerRepositoriesIn()   | Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[...]
INFO  .(TestDataCreator.java:242).run()                                      | ===== TestDataCreator: Loading schema and sample data from file: sampleDB-H2.sql

到目前为止,这工作正常。然后第一个测试针对加载的测试数据运行得非常好。但后来

第二次试运行失败

DEBUG .(StartupInfoLogger.java:56).logStarting()                             | Running with Spring Boot v2.2.4.RELEASE, Spring v5.2.3.RELEASE
INFO  .(SpringApplication.java:655).logStartupProfileInfo()                  | The following profiles are active: test
INFO  .(RepositoryConfigurationDelegate.java:127).registerRepositoriesIn()   | Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[...]
INFO  .(Dialect.java:172).<init>()                                           | HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
WARN  .(ExceptionHandlerLoggedImpl.java:27).handleException()                | GenerationTarget encountered exception accepting command : Error executing DDL "create sequence hibernate_sequence start with 1 increment by 1" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create sequence hibernate_sequence start with 1 increment by 1" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)

这就是我的测试类的样子

我的测试类没有@DirtiesContext 注释。

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class PollServiceTests  extends BaseTest { ... }

为什么hibernate会在这里生成一个DDL?这不是我从日志中看到的第二次运行的 TestDataCreator。

src/main/resources/appliation-test.yml

spring:
  jpa:
    generate-ddl: false
    hibernate:
      ddl-auto: none

spring.active.profiles == test 可以在日志中看到。这些设置已加载。

我希望所有测试都针对应该在启动时加载一次的固定数据集运行。

我错过了什么?使用 maven 运行测试有什么不同

【问题讨论】:

    标签: spring jpa spring-data


    【解决方案1】:

    经过大量调试,我找到了解决方案

    深藏在另一个application-&lt;env&gt;.yml 文件中,仍然有一个设置可以让休眠状态来创建数据库架构。

    # In dev we can create a schema.sql script for initializing a database later in other environments
    # https://stackoverflow.com/questions/37648395/how-to-see-the-schema-sql-ddl-in-spring-boot
    # https://thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2-1/
    javax:
      persistence:
        schema-generation:
          scripts:
            action: create
            create-target: build/my-db-schema.sql
          database:
            action: create    # Must be set if you want hibernate to create the actual schema in the DB in addition to the SQL file dump.
    

    注释掉后,一切正常

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 1970-01-01
      • 2017-01-10
      • 2018-02-20
      • 2016-08-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多