Maaf jika posting itu mengulangi banyak lainnya, saya telah melihat semuanya dan mencoba semua resep yang disarankan, namun masalahnya tetap ada.

Saya memiliki proyek SpringBoot dengan Grails build, dengan struktur berikut:

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

Build.gradle saya adalah sebagai berikut:

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 mengenali pengujian saya dengan benar:

enter image description here

Namun, ketika saya menjalankan tes di iJ, saya mendapatkan output berikut:

> 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

Ketika saya menjalankan di baris perintah sebagai gradlew clean iT -i, saya melihat output berikut:

...
> 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

Sejujurnya, saya mencoba semuanya di sana di bawah bulan, tetapi ada sesuatu yang salah. Adakah yang bisa membantu saya menemukan apa yang saya lewatkan?

0
Nestor Milyaev 10 Mei 2021, 13:53

1 menjawab

Jawaban Terbaik

Setelah 2 hari menggali, dan menemukan masalah ini: https://github.com/gradle/ gradle/issues/12618 dan mencoba contoh dari repositori ini: https://github.com/pkainulainen/gradle-examples

Saya datang dengan teori bahwa Gradle 6.x tidak cocok dengan junit-jupiter. Sayangnya, saya tidak memiliki perbaikan yang tepat, tetapi 2 solusi berikut tampaknya berhasil:

  1. Ganti Junit5 dengan Junit4
  2. Menggantikan
test {
    useJUnitPlatform()
}

Dengan

tasks.withType(Test) {
    useJUnitPlatform()
}

Ide yang lebih baik dipersilakan!

0
Nestor Milyaev 10 Mei 2021, 15:14