【问题标题】:How to upgrade JUnit-5 in spring-boot 1.5.10?如何在 spring-boot 1.5.10 中升级 JUnit-5?
【发布时间】:2019-04-27 14:28:46
【问题描述】:

我正在使用 spring-boot-1.5.10.RELEASE 并使用 spring-boot-starter-test 用于内置 JUnit-1.4 的单元测试。我想在我的项目中使用 JUnit-1.5。我浏览了互联网上的大部分搜索结果链接到 spring-boot-2.0我们不能将 JUnit-5 与 sprint-boot-1.5.X 一起使用吗?

GitHub 链接或提示将非常重要。

【问题讨论】:

  • 使用的是什么构建器?maven 还是 gradle?
  • 您好,非常感谢您的回复....我正在使用 Maven,抱歉没有提及
  • 没有简单的方法可以做到这一点。您可以查看 stackoverflow.com/questions/41747849/… 来帮助解决它,但我建议您坚持使用 JUnit 4,直到您可以升级到 Spring Boot 2.x,它对 JUnit 5 具有直接的内置支持。
  • @VelNaga 您要么需要转到 Spring Boot 2,要么使用类似 github.com/sbrannen/spring-test-junit5 的东西作为临时措施。就个人而言,我会坚持使用 JUnit 4(即使使用 Spring Boot 2.x,我也会继续使用它,因为在 JUnit 5 中我不需要使用任何东西)。

标签: java spring spring-boot junit


【解决方案1】:

要迁移,您可以查看此链接https://www.baeldung.com/junit-5-migration 使用 Junit 5 的依赖关系,https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-running-unit-tests-with-maven/ 如果你想使用spring boot starter test,应该排除与Junit 5相关的依赖项

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <version>1.5.1.RELEASE</version>
     <scope>test</scope>
     <exclusions>
          <exclusion> 
          <!-- declare the exclusion here -->
             <groupId>sample.junit</groupId>
             <artifactId>junit</artifactId>
          </exclusion>
     </exclusions> 
 </dependency>

并根据 Junit 5 版本添加它们,或者您可以声明版本

 <properties>
    <junit-jupiter-engine.version>5.1.0</junit-jupiter-engine.version>
    <junit-vintage-engine.version>5.1.0</junit-vintage-engine.version>
    <junit-platform-launcher.version>1.1.0</junit-platform-launcher.version>
    <junit-platform-runner.version>1.1.0</junit-platform-runner.version>
 </properties>

【讨论】:

  • 排除那些属性,只添加这些属性就够了吗?
  • 这是一个解决方案,我放了两个解决方案,可以解决你的问题..第二个比第一个好
  • 这对你有用吗,@JonathanJohx?从 JUnit 4 到 JUnit 5,“扩展”运行器的注释从 @RunWith 更改为 @ExtendWith。但是,后者不适用于 SpringRunner,它仅适用于 JUnit 4。那么您是如何让 Spring Boot 使用您的解决方案进行初始化的呢?
  • @TedM.Young 适用于迁移,现在它必须更改他的代码,你显然提到了:P
  • @JonathanJohx 抱歉,我不明白你的意思——我尝试了你的建议,但它对我不起作用。您能否提供一个完整的工作示例(带有使用@SpringBootTest 的测试)?
【解决方案2】:

如果你有一个Spring Framework 早于5.0 的项目或一个SpringBoot 早于2.0 版本的项目,那么你在spring 库的源代码中找不到SpringExtension 类。此扩展提供了使用 Spring 运行 JUnit5 集成测试的能力。

要解决这个问题,你需要添加一个 Sam Brannen 的依赖:

<dependency>
    <groupId>com.github.sbrannen</groupId>
    <artifactId>spring-test-junit5</artifactId>
    <version>1.2.0</version>
    <scope>test</scope>
</dependency>

您还需要在 pom 文件的 repositories 块中添加 jitpack:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 2016-11-29
    • 1970-01-01
    • 2021-12-26
    • 2022-08-19
    • 2021-01-18
    • 2019-10-10
    • 2018-08-18
    • 2023-04-03
    相关资源
    最近更新 更多