【发布时间】:2022-01-24 21:43:39
【问题描述】:
我的单元测试有一个奇怪的问题.. 我的目标是在 H2 数据库上而不是在我的 Mysql 数据库上运行测试..
实际上奇怪的是,当我点击 maven test 时,它会运行应用程序,尝试连接到 mysql.. 崩溃并且以 h2 数据库和单元测试开始失败..
@ExtendWith(SpringExtension.class)
@SpringBootTest
// @ActiveProfiles("test") // Without this it runs on my mysql and works.. ,
// with this annotation the behaviour is described above
@Transactional
class BelugaprojectsApplicationTests {
@Autowired
private ICheckConfigService iCheckConfigService;
@Autowired
private CheckConfigJpaRepository checkConfigJpaRepository;
@Test
void getAllCheckConfigDeploiement() {
assertThat(iCheckConfigService.getAllCheckConfigDeploiement(
Integer.parseInt(AppConstants.DEFAULT_PAGE_NUMBER),
Integer.parseInt(AppConstants.DEFAULT_PAGE_SIZE),
"id").getTotalElements() > 0);
}
}
属性
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
username: root
password:
jpa:
open-in-view: true
hibernate:
ddl-auto: create-drop
在激活测试配置文件的情况下运行它会抛出
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "HISTORIQUEDEPLOIEMENT" non trouvée
Table "HISTORIQUEDEPLOIEMENT" not found; SQL statement:
alter table historiquedeploiement add constraint FK134pkswiisobg18okjr9pegt7 foreign key (id_namespace) references namespace (id) [42102-200]
Table "CHECKCONFIGDEPLOIEMENT" non trouvée
Table "CHECKCONFIGDEPLOIEMENT" not found; SQL statement:
依赖关系
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
【问题讨论】:
-
属性文件的名称是什么,它在哪里?
-
application-test.yaml 位于 test/resources
标签: java spring-boot junit h2 junit5