运行 ./gradlew clean build 时在集成测试中出现初始化错误

问题描述 投票:0回答:1

我在我的 java Spring boot 项目中使用 test-Containers 编写了一些集成测试。我可以通过 intelliJ 运行测试用例,并且可以通过命令

./gradlew integrationTest
独立运行我的 gradle 任务来运行它们。

但是当我通过命令

./gradlew clean build
清理构建我的项目时,我收到初始化错误。完全例外:

IntegrationTest > initializationError FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:143
        Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at ConstructorResolver.java:800
            Caused by: org.springframework.beans.factory.BeanCreationException at ConstructorResolver.java:659
                Caused by: org.springframework.beans.BeanInstantiationException at SimpleInstantiationStrategy.java:171
                    Caused by: java.lang.IllegalStateException at BuildInfoFactory.java:47
                        Caused by: java.io.FileNotFoundException at ClassPathResource.java:211

我的文件结构如下所示:

src
|
|-- main
|-- test
|-- tests-integration
     |
     |-- java
     |-- resources

下面是我如何在 build.gradle 中定义集成测试任务的源代码

sourceSets {
    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file("src/tests-integration/java")
        }
        resources.srcDir file("src/tests-integration/resources")
    }
}

configurations {
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntimeOnly.extendsFrom testRuntimeOnly
}

task integrationTest(type: Test) {
    useJUnitPlatform()
    environment "APPLICATION_NAME", "{project_name}"
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath

    mustRunAfter test
    finalizedBy jacocoTestReport
}

integrationTest.configure {
    onlyIf { !project.hasProperty('skipIntegrationTests') }
}
check.dependsOn(integrationTest)
java spring-boot gradle integration-testing testcontainers
1个回答
0
投票

我给你的建议是:除非必要,否则不要干净地运行

我仔细查看了您的测试配置,它看起来很正确。

我相信可能有一个与

clean
相关的 gremlin 没有在所有其他任务之前运行,这看起来是一个已知问题,被认为在 7.4 版 (GitHub) 中得到了修复。也许您使用的是 7.4 之前的 Gradle 版本,或者您在某种程度上对
clean
有任务依赖性?如果您愿意,可以通过仔细查看任务执行顺序来调查这一点。

无论如何,除了特殊情况之外,没有必要运行

clean
。任务排序应负责在需要时更新任务的输入和输出,如果无法正常工作,则应对其进行修复,而不是使用
clean
作为膏药。

© www.soinside.com 2019 - 2024. All rights reserved.