【问题标题】:what's the difference between spring-boot-test vs spring-boot-starter-test?spring-boot-test 与 spring-boot-starter-test 有什么区别?
【发布时间】:2020-07-21 21:11:06
【问题描述】:

我正在处理一个项目,我看到定义了这些依赖项:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

但我不明白为什么有 2 个工件用于使用 Spring Boot 进行测试,它们之间有什么区别?也许是后者,我也在导入前者?

【问题讨论】:

    标签: spring spring-boot maven spring-test


    【解决方案1】:

    来自 Spring Boot 官方参考:

    Spring Boot 提供了许多实用程序和注释来帮助测试您的应用程序。测试支持由两个模块提供:spring-boot-test 包含核心项目,spring-boot-test-autoconfigure 支持自动配置测试。

    more details>>

    【讨论】:

      【解决方案2】:

      spring-boot-starter-test 是一个聚合的“入门包”,用于在 Spring 应用程序中经常一起用于测试的库。

      如最新版本参考documentation 中所述,spring-boot-starter-test 包含:

      • JUnit 5(包括向后兼容 JUnit 4 的老式引擎)

      • Spring 测试和 Spring Boot 测试 - 这是 spring-boot-test 依赖项)

      • AssertJ、Hamcrest、Mockito、JSONassert 和 JsonPath。

      您可以删除 spring-boot-test 依赖项的显式定义。

      【讨论】:

      • 所以我可以删除形成的dep?
      【解决方案3】:

      请查看 Maven 定义。包的内容在那里有详细说明。 spring-boot-starter-test 似乎是 spring-boot-test 的超集,因为 spring-boot-starter-test 依赖于 spring-boot-test。

      https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test/2.2.5.RELEASE

      https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-test/2.2.5.RELEASE

      【讨论】: