• 解决 IntelliJ IDEA 低版本与 Spring Boot 2.2+ 的测试兼容性问题


    解决 IntelliJ IDEA 低版本与 Spring Boot 2.2+ 使用 JUnit 5 的兼容性问题的三种思路:升级 IntelliJ IDEA 版本、使用 JUnit 4 进行单元测试、降低 Spring Boot 版本

    在使用 Spring Boot 2.2 版本以上时,默认使用 JUnit 5 进行测试。但是,如果您的 IntelliJ IDEA 版本低于 2017.3,可能会遇到以下错误信息:

    警告: TestEngine with ID 'junit-jupiter' failed to discover tests
    java.lang.AbstractMethodError: Method org/junit/platform/launcher/core/DefaultDiscoveryRequest.getFiltersByType(Ljava/lang/Class;)Ljava/util/List; is abstract
    ...
    Empty test suite.
    • 1

    解决思路有三个:

    1. 升级 IntelliJ IDEA 版本:JUnit 5 与 IntelliJ IDEA 2017.3 版本及更高版本兼容。建议升级到最新版本的 IntelliJ IDEA,以获得对 JUnit 5 的完全支持。新版本的 IntelliJ IDEA 提供了更好的集成和支持,能够更轻松地编写和运行 JUnit 5 测试。建议访问 IntelliJ IDEA 的官方网站,下载并安装最新版本的 IntelliJ IDEA。

    2. 如果因其他原因不想升级IntelliJ IDEA 版本,同时Spring Boot的版本也不调整,也可以使用 JUnit 4 进行单元测试:需要在 pom.xml 文件中做如下调整:

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.jupitergroupId>
                <artifactId>junit-jupiter-apiartifactId>
            exclusion>
        exclusions>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.13.2version>
        <scope>testscope>
    dependency>
    • 1

    测试类示例:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class Demo0611ApplicationTests 
    {

        @Test
        public void contextLoads() {
            // Your test logic here
        }

    }
    • 1

    通过以上调整,您可以使用 JUnit 4 进行单元测试。

    1. 如果不想升级IntelliJ IDEA 版本,也不想调整pom文件,则只要降低 Spring Boot 版本:如将 Spring Boot 版本退回到 2.2 以下,例如 2.1.3.RELEASE,这样默认就会使用 JUnit 4 进行测试,而不是 JUnit 5。

    以上是解决 IntelliJ IDEA 低版本与 Spring Boot 2.2+ 使用 JUnit 5 的兼容性问题的三种思路。大家可以根据实际情况选择适合的解决方案。

    本文由 mdnice 多平台发布

  • 相关阅读:
    xml笔记
    Android -- 每日一问:两个 Fragment 之间如何进行通信 ?
    hdfs常用命令行
    【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-Chapter20-正则表达式
    Java观察者模式之总有你想不到的知识
    Apache SkyWalking 监控 MySQL Server 实战
    Flutter学习1 - Android开发者快速上手
    【0141】【创建postgres后端进程】postgres进程process title的设计机制(10)
    JavaScript的路由
    Ansible——template模块
  • 原文地址:https://blog.csdn.net/fgliu/article/details/131154298