java - 未找到单独文件夹库中的 IntelliJ 集成测试 : Springboot with Gradle project configuration problem

标签 java spring-boot gradle intellij-idea

我有一个具有以下结构的 intelliJ Springboot Gradle 项目:

MyProject
├── build.gradle
└── src
    ├── integration (integration test sources root)
    │   ├── java 
    │   │   └── com...
    │   └── resources
    │
    ├── main (sources root)
    │   ├── java
    │   │   └── com...
    │   └── resources
    │
    ├── test (test sources root)
    │   └── java
    │       └── com...
    │...

我的 build.gradle 是这样的:

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

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

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

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

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

tasks.withType(Test) {
    useJUnitPlatform()
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'org.postgresql:postgresql'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.assertj:assertj-core'
}

现在,我在 src/test 中进行了一些单元测试,它们运行良好。

但是,对于集成测试,例如:

package com.bike;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BorrowMyBikeApplicationTests {

    @Test
    void contextLoads() {
    }
}

它在类路径中找不到任何库,因此例如对于@SpringBootTest,它建议Add library Gradle:spring-boot-test:2.4.3 to Classpath,对于 @Test,建议是 Add JUnit4/5 to classpath

放置在 src/test 中的相同测试工作顺利。

虽然我可以采纳这些建议,但每次 checkout 项目时我都必须添加这些依赖项。我更喜欢它在 integration 文件夹中的工作方式与在 test 文件夹中的工作方式相同。

我缺少什么以及如何完成配置才能开箱即用?

最佳答案

拖网 https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests ,我找到了答案:

我缺少集成测试的配置,如:

configurations {
    integrationImplementation.extendsFrom testImplementation
    integrationRuntimeOnly.extendsFrom testRuntimeOnly
} 

请注意 integrationImplementation.extendsFrom 应该是 [集成测试源集的名称]Implementation.extedsFrom(在我的例子中是 intgergation), integrationRuntimeOnly.extendsFrom

相同

可选:您还可以扩展您的 sourceSets(即使没有它似乎也能工作,但我发现这样更干净):

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

关于java - 未找到单独文件夹库中的 IntelliJ 集成测试 : Springboot with Gradle project configuration problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66785405/

相关文章:

java - 使用 xpath 单击隐藏按钮时出现陈旧元素引用 : element is not attached to the page,

java - 无法使用 JavaMail 连接到 Hotmail

java - 无法让 Intellij 使用 AspectJ 编译器

java - 作为IT公司中较新的应用程序开发人员,是否足够了解所有编程语言的基础知识?

java - bodyToMono ParameterizedTypeReference 和 bodyToFlux 有什么区别

java - 如何序列化函数返回的 rx.Single 对象?

java - Spring Cloud Config-属性解密在客户端不起作用

android - 为什么 Android Studio 找不到我的资源?

gradle - Gradle:在Jenkins中找不到方法jacocoTestCoverageVerification()

grails - Gradle 2.3中的Jcenter()异常