gpt4 book ai didi

java - Gradle 在单元测试期间运行 flywayMigration

转载 作者:行者123 更新时间:2023-12-03 03:54:54 25 4
gpt4 key购买 nike

我正在开发 spring boot/gradle 项目,由于某种原因,Gradle 在单元测试期间运行 flywayMigrate 任务,所以它失败了。

Error occurred while executing flywayMigrate. Connection refused 

我用 ./gradlew clean test 运行测试

我的 build.gradle 文件:
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'nu.studer.jooq' version '4.1'
id "org.flywaydb.flyway" version "6.2.0"
}

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

ext {
dbUrl = "jdbc:postgresql://localhost:5432/test"
dbUsername = 'admin'
dbPassword = 'admin'
dbDriver = 'org.postgresql.Driver'
dbSchema = 'public'
}

repositories {
mavenCentral()
}


dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-jooq'
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'javax.xml.bind:jaxb-api:2.3.0'
implementation 'org.flywaydb:flyway-core'
implementation 'org.postgresql:postgresql:42.2.9'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
jooqRuntime 'org.postgresql:postgresql:42.2.9'
compile 'org.jooq:jooq'
}

test {
useJUnit()
}

jooq {
version = '3.11.0'
edition = 'OSS'
sample(sourceSets.main) {
jdbc {
driver = dbDriver
url = dbUrl
user = dbUsername
password = dbPassword
schema = dbSchema
}
generator {
database {
inputSchema = dbSchema
excludes = 'databasechangelog|databasechangeloglock'
}
generate {
relations = false
deprecated = false
records = true
immutablePojos = false
fluentSetters = false
javaTimeTypes = true
}
target {
directory = 'build/generated/sources/jooq'
packageName = 'com.test.generated.models'
}
}
}
}

flyway {
url = dbUrl
user = dbUsername
password = dbPassword
schemas = [dbSchema]
locations = ["filesystem:$project.projectDir/src/main/resources/db/migration"]
}

generateSampleJooqSchemaSource.dependsOn flywayMigrate

我不尝试运行集成测试。只是单位,所以我不需要那里的数据库连接。 FlywayMigration 与 gradlew bootRun 配合得很好

最佳答案

“build.gradle”的飞行路径不是正在运行的飞行路径。

Spring boot 自动配置 flyway,因为您在依赖项中有 flyway 核心。
默认情况下,它使用您的 application.properties/application.yml 来读取 jdbc 参数。

所以我对你的建议是在测试资源上创建一个新的应用程序属性并设置 spring.flyway.enabled=false .

引用:
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

https://flywaydb.org/documentation/plugins/springboot

关于java - Gradle 在单元测试期间运行 flywayMigration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61465228/

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