无法集成测试gradle多模块Spring-Boot-Application

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

在一个Gradle多模块项目中,在它自己的模块中的引导,我无法使用。MockMvc因为它需要引用bootstrapping-module。我不知道是不是我配置错了什么。基本结构是

  • 模块: 一个包含一些REST服务的模块,需要在starter上设置一个testImplementation-Dependency。
  • starter: 引导模块,应用了spring-boot-plugin,并依赖于模块。

我已经建立了一个最小的例子,在 github 使用Spring-Boot 2.3.1.RELEASE和Gradle 6.4,配置如下。

.settings.gradle.kts,使用Spring-Boot 2.3.1.RELEASE和Gradle 6.4,配置如下

rootProject.name = "spring-multimodule-integrationtest"
include("starter", "module")

.build.gradle.kts

subprojects {
    repositories {
        jcenter()
    }
    dependencies {
        apply(plugin = "java-library")
        "testImplementation"("junit:junit:4.12")
    }
}

.starterbuild.gradle.kts。

plugins {
    id("org.springframework.boot") version "2.3.1.RELEASE"
}

dependencies {
    implementation(project(":module"))
}

.modulebuild.gradle.kts。

dependencies {
    testImplementation(project(":starter"))
}

启动器-模块只包含一个单一的 "Starter "类,引用了 "Starter"。模块-模块。

public class Starter {
    public String info() { return "starter"; }
    public static void main(String[] args) {
        System.out.println(new Starter().info() + " and " + new Module().info());
    }
}

该模块: 模块-模块(*叹息我应该为这个模块选择一个不同的名字)只包含这个实现类。

public class Module {
    public String info() { return "module"; }
}

此外, 模块模块中的test-class做了集成测试,这个代码在".starterbuild.gradle.kts "中应用spring-boot-plugin之前运行正常。

public class IntegrationTest
{
    @Test public void testSomeLibraryMethod() {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        System.setOut(new PrintStream(out));
        Starter.main(new String[0]);
        assertEquals("starter and module\n", out.toString());
    }
}

这段代码在".starterbuild.gradle.kts "中应用spring-boot-plugin之前运行良好。当 "clean test "的任务在shell上发出时,我得到的是:"我的测试结果是什么?

❯ ./gradlew clean test

> Task :module:test FAILED

de.kramhal.multi.IntegrationTest > testSomeLibraryMethod FAILED
    java.lang.NoClassDefFoundError at IntegrationTest.java:17
        Caused by: java.lang.ClassNotFoundException at IntegrationTest.java:17

1 test completed, 1 failed

当在IDE(确切的说是IntelliJ)中执行测试时,这个问题不会发生。

我已经尝试过使用弹簧依赖性管理,但没有成功,正如这个建议。回答 以及其他几个答案中)。

我做错了什么?

java spring spring-boot gradle multi-module
1个回答
0
投票

首先,我建议调整你的项目结构,这样你就不会有循环的依赖关系了。就像现在这样,为了构建 starter,你需要建立 module. 而为了测试 module,你需要建立 starter. Gradle可以做到,但通常都是闻之色变。

在故障排除方面:当你遇到这样的测试失败时,看看测试报告,因为它有完整的栈跟踪。你应该看到,它抱怨说它找不到 Starter 类 (Caused by: java.lang.ClassNotFoundException: de.kramhal.multi.Starter),这是在的原因。starter 模块。

你提到了 spring-dependency-management 插件,但这只适用于管理Maven依赖关系,而不是像这样的项目依赖关系。所以它在这里没有帮助。

我不完全确定这是否是Windows特有的,因为我记得之前有一些关于性能的讨论,当有很多类的时候。但我相信 java-library 插件会在其他项目中寻找jar文件,而不是编译类的文件夹。这对你来说是个问题,因为 spring-boot 插件默认会禁用标准的 jar 任务,并通过 "fat "创建一个jar文件。bootJar 任务。因为你既需要将应用程序打包成独立运行的fat jar,也需要将普通jar作为依赖关系使用,所以你需要对 starter 项目(Kotlin DSL)。

tasks {
    jar {
        enabled = true
    }
    bootJar {
        archiveClassifier.set("boot")
    }
}

这将启用普通的jar文件,但由于名称会与Kotlin DSL产生的文件冲突。bootJar 任务,你需要重命名其中一个。我选择了重命名 bootJar 一个。

我不知道为什么你在IntelliJ中的测试能正常工作,因为默认情况下,它应该把所有的事情都委托给Gradle来做,但也许你是旧版本,或者做了一些手动配置,让IntelliJ同时编译和运行你的测试。但也许你有一个旧版本,或者做了一些手动配置,让IntelliJ同时编译和运行你的测试。

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