• There are test failures.【非常详细,已解决】


    大家好
    授人以鱼不如授人以渔

    maven打包为什么会出现这个错误,打包又干了些啥?

    一 原因分析

    我的test类,总共也没几行代码,居然会出错,我也是醉了

    package cn.fox.mydemo;
    
    import cn.fox.mydemo.domain.entity.MyUserEntity;
    import cn.fox.mydemo.service.impl.MyUserServiceImpl;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import lombok.extern.slf4j.Slf4j;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @Slf4j
    @SpringBootTest
    class MyDemoApplicationTests {
    
        @Autowired
        private MyUserServiceImpl myUserServiceImpl;
    
        @Test
        public void t2() {
            MyUserEntity one = myUserServiceImpl.getOne(new QueryWrapper<MyUserEntity>().lambda()
                    .eq(MyUserEntity::getName, "白"));
            System.out.println(one.getName());
        }
        @Test
        public void t1() {
            log.info("info哈哈哈");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    然后出现了这个问题

    There are test failures.
    
    Please refer to D:\fox\project\my-demo\target\surefire-reports for the individual test results.
    Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    点击test如下
    在这里插入图片描述
    然后就停止编译了

    二 maven打包过程

    先看一下maven的打包日志,选择项目名字,可以查看最全的日志

    1 首先是运行了TESTS

    在这里插入图片描述

    2 然后启动SpringBoot项目,因为我在test文件上面加了@SpringBootTest注解,不然我的serviceImlpl无法注入

    在这里插入图片描述

    3 接着注入mybatis,执行有@test注解的方法

    在这里插入图片描述

    4 代码有个逻辑错误,将空指针异常抛出来,maven编译失败

    在这里插入图片描述

    三 解决办法

    1 注释掉@Test、或者整个文件都注释掉

    如果知道问题点的话的话可以注test(甚至解决这个问题都行,但我觉得没必要。如果项目很多人合作,test里面有大量的代码,而且都不是必要的代码),代码太多可以注释掉整个文件的代码

    2 忽略测试的失败

    在pom.xml文件添加下面的代码,maven打包的时候忽略测试的失败(也可以这样做,但不是最优的解决方案)

    	<build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-surefire-pluginartifactId>
                    <configuration>
                        <testFailureIgnore>truetestFailureIgnore>
                    configuration>
                plugin>
            plugins>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3 打包跳过所有test文件【建议】

    idea工具可以选择maven选项功能,选中小闪电,下面的test就会变为灰色,意思是打包的时候跳过test
    在这里插入图片描述
    maven命令为

    -DskipTests=true
    
    • 1

    再次打包就没问题了
    在这里插入图片描述

  • 相关阅读:
    商都区域内蒙古盐碱地治理 国稻种芯-封兴毅:水稻种植破题
    java基于ssm+vue的水果果蔬购物商城
    完全自定义MaterialButtonToggleGroup颜色。
    Android - Context
    这才是Git的正确学习方式
    Vue路由&&无痕浏览 - nodeJs环境搭建
    教你用python制作人脸卡通画(附源码)
    Apache Doris 行列转换可以这样玩
    【路径规划】基于matlab AI抗疫服务移动机器人路径规划系统【含Matlab源码 2096期】
    B. Reverse Binary Strings
  • 原文地址:https://blog.csdn.net/lh155136/article/details/126302063