【发布时间】:2021-10-27 14:28:24
【问题描述】:
我的项目中有一个 parameters 模块,其他模块依赖于该模块。我想以单独的方式为此模块编写集成测试。这个模块是一个基于 spring 的模块,并且有一些与 db 相关的逻辑,尽管 db 迁移脚本在这个模块中不可用,因为它超出了它的职责范围。顺便说一句,内存中的 H2 实例用于测试目的。
我想要实现的是让 spring/hibernate 基于此模块中存在的单个 @Entity 类创建 DB 表,它被称为 ParameterEntity。
它是这样定义的:
@Entity
@Table(name = "SYSTEM_PARAMETERS")
public class ParameterEntity {
@Id
@Column(name = "id")
private String id;
@Column(name = "value")
private String value;
// accessors go here...
}
在我的application-test.yml 文件中,我提供了以下道具:
spring:
jpa:
hibernate:
ddl-auto: create
database: h2
show-sql: true
并像这样定义集成测试类:
@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties
@EntityScan("path.to.package.where.parameter.entity.resides")
@ConstextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
@ActiveProfiles("test")
@TestInstance(Lifecycle.PER_CLASS)
public class ParametersIntegrationTest {
@Autowired
private ParameterWarden warden;
@BeforeEach
public void setUp() {
warden.initialize();
}
@Configuration
@ComponentScan("base.module.package")
static class TestConfiguration {
// here comes some test beans which will be used in testing purposes only
}
}
在@BeforeEach 方法中,ParameterWarden 类调用存储库类,后者又对数据库进行一些调用以从数据库中检索参数实体,这些调用失败,因为缺少SYSTEM_PARAMETERS。
请任何人告诉我我在这里缺少什么以及如何根据我的项目中存在的实体使 spring 或 hibernate 创建表。即使是我可以调试的地方也很高兴知道。
似乎我需要另一个可以触发此功能的神奇事物,但我无法弄清楚我到底需要什么。
非常感谢任何帮助,非常感谢您的宝贵时间!
附言我不能使用@SpringBootTest 注释,因为这个模块只使用了一些弹簧特性,而不是弹簧启动应用程序本身。它在另一个 Spring Boot 应用程序中用作依赖项。
【问题讨论】:
标签: spring spring-boot hibernate testing h2