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

标签 java spring-boot unit-testing gradle flyway

我正在开发 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/

相关文章:

java - 以 .jar 形式执行项目时无法找到 bluecove jar

java - 为什么我尝试使用 getActionCommand 时出现空指针异常

java - 集成 js 随机化器,显示可以用 css 设置样式的结果。

java - SQLException : Field doesn't have a default value

unit-testing - RxJS Redux-Observables 测试 retryWhen inside an epic

java - EasyMock 期望使用 void 方法和对象数组作为参数?

unit-testing - 如何将TestNG测试结果分配给用例?

java - ArrayLists 是如何在 Java 中实现的?

java - CXF 故障转移管道即时修改 - 保证相同的 SSL session 和客户端线程安全?

spring - 在 Grails 3 中将包添加到组件扫描的正确方法