首页 > 其他分享 >testng + mockito + spring boot test 基本操作

testng + mockito + spring boot test 基本操作

时间:2024-06-22 19:31:40浏览次数:11  
标签:plugin spring mockito maven version thresh org 基本操作 jacoco

代码地址

https://gitee.com/bzrj/thresh-boot

如何使用

  1. thresh-dependencies 目录执行 mvn clean install
  2. 在跟目录执行 make

效果

jacoco

image

image

allure

image

image

关键配置

thresh-test

  1. 此模块包含了测试需要的依赖
  2. 定义了两个 testng 监听

thresh-report

此模块专门用于聚合 jacoco 和 allure 的报告

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.laolang.thresh</groupId>
        <artifactId>thresh-boot</artifactId>
        <version>${revision}</version>
    </parent>

    <artifactId>thresh-report</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.laolang.thresh</groupId>
            <artifactId>thresh-module-system-biz</artifactId>
        </dependency>
        <dependency>
            <groupId>com.laolang.thresh</groupId>
            <artifactId>thresh-module-auth-biz</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 生成聚合报告 -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

模块 pom

注意:
由于使用了 powermock , 所以需要使用 jacoco 的 offline 模式

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>default-instrument</id>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-restore-instrumented-classes</id>
                        <goals>
                            <goal>restore-instrumented-classes</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>pre-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-test</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/jacoco.exec</dataFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <argLine>
                        -Dfile.encoding=UTF-8
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <testFailureIgnore>true</testFailureIgnore>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
                    </systemPropertyVariables>
                    <!--生成allure-result的目录-->
                    <systemProperties>
                        <!--是否忽略html,解释见下图。与之后在reportNg报告上显示截图相关。当前已经使用allure了,这里可以直接去掉啦-->
                        <!--<org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output>-->
                        <!--定义输出在项目 target 目录-->
                        <property>
                            <name>allure.results.directory</name>
                            <value>target/allure-results</value>
                        </property>
                    </systemProperties>
                    <suiteXmlFiles>
                        <!--该文件位于工程根目录时,直接填写名字,其它位置要加上路径-->
                        <suiteXmlFile>${xmlFileName}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

根目录

用于配置插件依赖

    <build>
        <finalName>${project.artifactId}</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco-maven-plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

标签:plugin,spring,mockito,maven,version,thresh,org,基本操作,jacoco
From: https://www.cnblogs.com/bibleghost/p/18262642

相关文章

  • SpringBoot 过滤器更改 Request body ,并实现数据解密
    客户端、服务端网络通信,为了安全,会对报文数据进行加解密操作。在SpringBoot项目中,最好使用参考AOP思想,加解密与Controller业务逻辑解耦,互不影响。以解密为例:需要在request请求到达Controller之前进行拦截,获取请求body中的密文并对其进行解密,然后把解密后的明文重新设置到request......
  • 深入探索B树:基本操作与应用解析
    在计算机科学中,B树是一种自平衡的树形数据结构,广泛用于数据库和文件系统的索引结构。它能够提供高效率的数据检索、插入和删除操作,特别适合于磁盘I/O密集型的应用场景。本文将详细探讨B树的基本操作,包括B树的定义、特性、插入、删除、分裂和合并等,以及它们在实际应用中的重......
  • JAVA学习笔记DAY10——SpringBoot基础
    文章目录SpringBoot3介绍SpringBoot快速入门@SpringBootApplicationSpringBoot配置文件统一配置管理Yaml配置优势tipsSpringBoot整合SpringMVC静态资源拦截器interceptorSpringBoot整合DruidSpringBoot整合MybatisSpringBoot整合txaopSpringBoot打包......
  • 基于springboot实现知识管理系统项目【项目源码+论文说明】计算机毕业设计
    摘要随着信息互联网信息的飞速发展,无纸化作业变成了一种趋势,针对这个问题开发一个专门适应师生作业交流形式的网站。本文介绍了知识管理系统的开发全过程。通过分析企业对于知识管理系统的需求,创建了一个计算机管理知识管理系统的方案。文章介绍了知识管理系统的系统分析......
  • 基于springboot实现酒店客房管理系统项目【项目源码+论文说明】计算机毕业设计
    摘 要随着人们的物质水平的提高,旅游业和酒店业发展的速度越来越快。近年来,市面上酒店的数量和规模都在不断增加,如何提高酒店的管理效率和服务质量成为了一个重要的问题。伴随着信息技术的发展,基于互联网的酒店客房管理系统已经成为了酒店管理过程中的一个重要的手段。这......
  • SpringBoot 3.x 结合 Swagger3 (Knife4j )踩坑实录
    SpringBoot3.x+Swagger3踩坑实录我的是springboot版本是:3.2.2<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.2</version>......
  • spring头部命名空间配置
    头部约束文件配置<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www......
  • SpringTask Cron表达式
    Cron表达式格式1.Cron表达式格式Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:秒分时一个月第几天月一个星期第几天年(1)SecondsMinutesHoursDayofMonthMonthDayofWeekYear(2)Seconds......
  • Spring Boot 源码分析五:Spring Boot AutoConfiguration 自动配置机制
    1.引言在前几篇文章中,我们探讨了SpringBoot的启动流程及其扩展机制。在本篇文章中,我们将深入分析SpringBoot的自动配置(AutoConfiguration)机制,这是SpringBoot最具特色和强大的功能之一。2.自动配置概述SpringBoot的自动配置机制旨在根据项目中的类路径和配置属性,自......
  • java毕业设计之在线考试系统(springboot完整源码+说明文档+演示视频)
    1项目介绍本系统主要包括管理员和用户两个角色组成;主要包括首页、个人中心、用户管理、教师管理、课程信息管理、班级信息管理、试题管理、在线试题管理、考试管理等功能的管理系统。2、项目技术项目后端框架:Java+ssm项目前端框架:vue2,ssm3、开发环境springboot环境......