【发布时间】: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