【问题标题】:Spring Boot 2.1.0 has JUnit5 dependencies, but how to get rid of it?Spring Boot 2.1.0 有 JUnit5 依赖,但是如何摆脱它呢?
【发布时间】:2019-04-18 13:54:13
【问题描述】:

我刚刚升级了我的项目以使用 Spring Boot 2.1.0(之前是 2.0.x) 我有编译警告:

[WARNING] Cannot find annotation method 'value()' in type 'org.junit.jupiter.api.extension.ExtendWith': class file for org.junit.jupiter.api.extension.ExtendWith not found

我可以添加依赖 org.junit.jupiter / junit-jupiter-api 来解决警告,但我觉得这是一个'hack'。

我不想看到该警告(尤其是我的项目将警告视为错误)并且我不想用不必要的依赖项污染我的项目。

我正在使用 Maven,但我可以看到有人在使用 Gradle 时遇到了同样的问题 https://www.reddit.com/r/java/comments/9sogxf/spring_boot_210_released_now_with_java_11_support/

【问题讨论】:

  • 能否将 POM 文件添加到问题中?
  • 你在使用maven/gradle/...吗?
  • Maven,我只更改了 spring-boot 版本。|你想看到“有效的 POM”还是只是为了一个项目? (这会很困难,因为版本由 spring 和我的父 POM 管理)
  • 如果你正在升级你的 spring boot jar 版本,那么我认为你应该考虑在需要保持向后兼容性的地方升级依赖项的版本。从长远来看,这将有所帮助。 mvnrepository.com/artifact/org.springframework.boot/spring-boot/…
  • 问题是,例如@DataJpaTest@SpringBootTest 注释用@ExtendWith 注释,这是JUnit 5 的一部分,所以看起来你必须在类路径中使用JUnit 5 API,即使你没有使用它。

标签: java spring-boot spring-boot-test


【解决方案1】:

如果你使用@SpringBootTest来设置classes=

@SpringBootTest(classes = {
        MyAutoConfiguration.class,
        MyAutoConfigurationIntegrationTest.TestContextConfiguration.class
    })

答案在其api documentation 中有所提及:

可以在运行 Spring Boot 的测试类上指定的注解 基于测试。提供以下功能 常规 Spring TestContext 框架:

在没有指定的时候使用 SpringBootContextLoader 作为默认的 ContextLoader @ContextConfiguration(loader=...) 已定义。

自动搜索 对于不使用嵌套@Configuration 时的@SpringBootConfiguration, 并且没有指定明确的类。

你可以使用不依赖于Junit5的@ExtendsWith@ContextConfiguration

@RunWith(SpringRunner.class)
@ContextConfiguration(loader = SpringBootContextLoader.class,
    classes = {
        MyAutoConfiguration.class,
        MyAutoConfigurationIntegrationTest.TestContextConfiguration.class
    })

【讨论】:

    【解决方案2】:

    如果您将 org.junit.jupiter:junit-jupiter-api 依赖项添加到您的项目中,警告将会消失。 它不应该受到伤害,因为它只是 API jar 并且仅在测试范围内。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-18
      • 2016-09-28
      • 2023-03-14
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2022-01-16
      • 2020-11-03
      相关资源
      最近更新 更多