自定义文件夹中的集成测试:任务“:integrationTest”执行失败。没有找到给定的测试包括

Integration test in custom folder: Execution failed for task ':integrationTest'. No tests found for given includes

抱歉,如果 post 重复了很多其他的,我已经看过所有这些并尝试了所有建议的食谱,但问题仍然存在。

我有一个使用 Grails 构建的 SpringBoot 项目,具有以下结构:

mybike-rest
    src
        integration
            java
                com.bike
                    BorrowMyBikeApplicationTest
            resources
        main
            java
            ...
        test
            java
            ...

我的build.gradle如下:

plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'idea'
}

group = 'java.bike'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'

sourceSets {
    main {
        java.srcDirs('./src/main/java')
        resources.srcDirs('./src/main/resources')
    }
    test {
        java.srcDirs('./src/test/java')
        resources.srcDirs('./src/test/resources')
    }
    integrationTest {
        java.srcDirs('./src/integration/java')
        resources.srcDirs('./src/integration/resources')
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
}

configurations{
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntime.extendsFrom testRuntime
}

task integrationTest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    outputs.upToDateWhen { false }
    testLogging { events "passed", "skipped", "failed" } // This is not needed, but I like to see which tests have run
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
}

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.projectlombok:lombok'
    compile 'org.flywaydb:flyway-core'
    runtimeOnly 'org.postgresql:postgresql'
    runtimeOnly 'com.h2database:h2'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'org.junit.jupiter:junit-jupiter'
    testCompile 'org.springframework.data:spring-data-commons'
    testCompile 'org.assertj:assertj-core'

    integrationTestCompile sourceSets.main.output
    integrationTestCompile sourceSets.test.output
    integrationTestCompile configurations.compile
    integrationTestCompile configurations.testCompile
    integrationTestRuntime configurations.runtime
    integrationTestRuntime configurations.testRuntime
    integrationTestAnnotationProcessor configurations.testAnnotationProcessor

    annotationProcessor 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'
}

IntelliJ 正确识别我的测试:

然而,当我 运行 在 iJ 中进行测试时,我得到以下输出:

> Task :cleanIntegrationTest
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources UP-TO-DATE
> Task :testClasses UP-TO-DATE
> Task :compileIntegrationTestJava UP-TO-DATE
> Task :processIntegrationTestResources
> Task :integrationTestClasses
> Task :integrationTest FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':integrationTest'.
> No tests found for given includes: [com.bike.BorrowMyBikeApplicationTest](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.8.3/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 1s
8 actionable tasks: 3 executed, 5 up-to-date

当我在命令行中 运行 作为 gradlew clean iT -i 时,我看到以下输出:

...
> Task :compileIntegrationTestJava
file or directory '/home/nmilyaev/Work/Intellij/mybike-rest/src/integrationTest/java', not found
Excluding []
Excluding []
Caching disabled for task ':compileIntegrationTestJava' because:
  Build cache is disabled
Task ':compileIntegrationTestJava' is not up-to-date because:
  Output property 'destinationDirectory' file /home/nmilyaev/Work/Intellij/mybike-rest/build/classes/java/integrationTest has been removed.
  Output property 'destinationDirectory' file /home/nmilyaev/Work/Intellij/mybike-rest/build/classes/java/integrationTest/com has been removed.
  Output property 'destinationDirectory' file /home/nmilyaev/Work/Intellij/mybike-rest/build/classes/java/integrationTest/com/bike has been removed.
The input changes require a full rebuild for incremental task ':compileIntegrationTestJava'.
Full recompilation is required because no incremental change information is available. This is usually caused by clean builds or changing compiler arguments.
file or directory '/home/nmilyaev/Work/Intellij/mybike-rest/src/integrationTest/java', not found
Compiling with JDK Java compiler API.
Created classpath snapshot for incremental compilation in 0.006 secs. 1 duplicate classes found in classpath (see all with --debug).
:compileIntegrationTestJava (Thread[Daemon worker Thread 10,5,main]) completed. Took 0.643 secs.
:processIntegrationTestResources (Thread[Execution worker for ':' Thread 3,5,main]) started.

> Task :processIntegrationTestResources
file or directory '/home/nmilyaev/Work/Intellij/mybike-rest/src/integrationTest/resources', not found
Caching disabled for task ':processIntegrationTestResources' because:
  Build cache is disabled
Task ':processIntegrationTestResources' is not up-to-date because:
  Output property 'destinationDir' file /home/nmilyaev/Work/Intellij/mybike-rest/build/resources/integrationTest has been removed.
  Output property 'destinationDir' file /home/nmilyaev/Work/Intellij/mybike-rest/build/resources/integrationTest/application.properties has been removed.
file or directory '/home/nmilyaev/Work/Intellij/mybike-rest/src/integrationTest/resources', not found
:processIntegrationTestResources (Thread[Execution worker for ':' Thread 3,5,main]) completed. Took 0.019 secs.
:integrationTestClasses (Thread[Execution worker for ':' Thread 3,5,main]) started.

> Task :integrationTestClasses
Skipping task ':integrationTestClasses' as it has no actions.
:integrationTestClasses (Thread[Execution worker for ':' Thread 3,5,main]) completed. Took 0.0 secs.
:integrationTest (Thread[Execution worker for ':' Thread 3,5,main]) started.

> Task :integrationTest
Excluding []
Caching disabled for task ':integrationTest' because:
  Build cache is disabled
Task ':integrationTest' is not up-to-date because:
  Task.upToDateWhen is false.
Finished generating test XML results (0.0 secs) into: /home/nmilyaev/Work/Intellij/mybike-rest/build/test-results/integrationTest
Generating HTML test report...
Finished generating test html results (0.003 secs) into: /home/nmilyaev/Work/Intellij/mybike-rest/build/reports/tests/integrationTest
:integrationTest (Thread[Execution worker for ':' Thread 3,5,main]) completed. Took 0.149 secs.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.8.3/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 4s

老实说,我在月球下尝试了一切,但有些不对劲。谁能帮我找出我遗漏了什么?

经过 2 天的挖掘,遇到了这个问题:https://github.com/gradle/gradle/issues/12618 并从这个存储库中尝试一个例子:https://github.com/pkainulainen/gradle-examples

我提出了一个理论,即 Gradle 6.x 不能很好地与 junit-jupiter 配合使用。遗憾的是,我没有适当的修复方法,但以下 2 个解决方法似乎有效:

  1. 用 Junit4 替换 Junit5
  2. 替换
test {
    useJUnitPlatform()
}

tasks.withType(Test) {
    useJUnitPlatform()
}

欢迎提出更好的想法!

感谢您的回答,@nestor-milyaev。我的项目结构与您的项目类似,这是我的场景:

tasks.withType(Test) {
    useJUnitPlatform {
        excludeEngines 'junit-vintage'
    }
    testLogging {
        events "FAILED", "SKIPPED"
    }
}

现在 IT 和 UT 一样 运行。