如何防止springjpa在运行测试时创建数据库模式



我有一个中等大小的弹簧引导应用程序。当我在IntelliJ中运行测试时,一切都很好。

我的spring应用程序从CommandLineRunner中的testdata.sql文件加载一组静态测试数据。该文件创建DB模式并加载准备好的数据。这很快,效果很好。这样,我的测试总是针对同一组数据运行。(测试夹具。(

但是当我用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==测试,如日志中所示。这些设置已加载。

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

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

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

在另一个application-<env>.yml文件中,仍然有一个设置使hibernate能够创建数据库模式。

# 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.

在评论了这一点之后,一切都很好

最新更新