gpt4 book ai didi

spring-boot - Spring Boot - 使用 Flyway 和 H2 数据库进行测试

转载 作者:行者123 更新时间:2023-11-28 20:27:07 26 4
gpt4 key购买 nike

我正在使用 Cucumber 编写验收测试代码,我想使用 H2 数据库进行测试。

application-test.properties 看起来像:

server.port:8090

spring.jpa.database=H2
spring.database.driverClassName=org.h2.Driver
spring.datasource.url:jdbc:h2:mem:database_user;DB_CLOSE_ON_EXIT=FALSE
flyway.locations=classpath:resources/db/migration
flyway.enabled=true

spring.datasource.username:SA
spring.datasource.password:

spring.h2.console.enabled=true
spring.jpa.show-sql=true

security.basic.enabled:false
spring.application.name=userService

在目录 resources/db/migration 中,我有一个包含这些脚本的 sql 文件:

create table user_image(
id int unsigned not null AUTO_INCREMENT,
url varchar(1000) not null,
s3_key varchar(200) not null,
PRIMARY KEY (id)
);


create table user (
id int unsigned not null AUTO_INCREMENT,
email varchar(50) not null,
password varchar(100) not null,
first_name varchar(50) not null,
last_name varchar(50) not null,
description varchar(50),
phone_number varchar(50),
user_image_id int unsigned,
need_refresh_pass boolean not null,
PRIMARY KEY (id),
CONSTRAINT fk_user_image FOREIGN KEY (user_image_id)
REFERENCES user_image(id)
);

但是当我运行测试时,H2 使用默认格式创建模式而不是使用脚本:

enter image description here

如您所见,所有 VARCHAR 都是使用 255 大小创建的,而不是实际值。

你能帮我把 flyway 和 H2 集成吗?

谢谢!

最佳答案

1- 确保您的休眠 DDL 生成已禁用:

spring.jpa.hibernate.ddl-auto=none

2- 确保您的 SQL 迁移脚本的名称符合 flyway 的约定。即

V1__create_user_table_for_test.sql

关于spring-boot - Spring Boot - 使用 Flyway 和 H2 数据库进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52049592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com