当前位置: 首页 > news >正文

关于测试组件junit切换testng的示例以及切换方式分享

文章目录

    • 概要
    • 首先看看junit和testng的区别
    • 实践篇
      • 摸拟业务逻辑代码
        • 简单对象
        • 数据层摸拟类
        • 业务逻辑层摸拟类
        • 后台任务摸拟类
      • 基于spring+mock+junit
      • 基于spring+mock+testng
    • 示例的差异点
        • junit与testng的主要变动不大,有以下几个点需要注意
        • 注解部分
        • 在before,after中
        • testng多出按配置执行功能
        • 附上关于mock 新旧写法改进
    • 小结

概要

本文作者之前写单元测试都是使用junit
场景有以下三种场景
仅junit
spring+junit
mock+spring+junit

本文会用第三种场景写简单的实例列出junit和testng的代码相关说明
并会将涉及的修改点一一说明
目的帮助大家了解testng及具体的切换方式

首先看看junit和testng的区别

JUnit和TestNG是两种流行的Java测试框架,用于测试Java应用程序中的代码。它们具有以下区别:

  1. 组织方式:JUnit使用Annotations来标注测试方法,而TestNG使用XML文件来组织测试。

  2. 支持的测试类型:JUnit 4支持单元测试,而TestNG支持功能测试、集成测试和端到端测试。

  3. 并发测试:TestNG支持并发测试,可以在同一时间运行多个测试用例,而JUnit不支持并发测试。

  4. 数据提供者:TestNG支持数据提供者,可以在不同参数上运行相同的测试用例,而JUnit不支持数据提供者。

  5. 测试套件:TestNG支持测试套件,可以组织不同的测试用例,而JUnit不支持测试套件。

  6. 依赖测试:TestNG支持依赖测试,可以在一组测试之前运行必需的测试,而JUnit不支持依赖测试。JUnit和TestNG是两种流行的Java测试框架,用于测试Java应用程序中的代码。它们具有以下区别:

实践篇

摸拟业务逻辑代码

场景,有三层以上代码层次的业务场景,需要摸拟最底层数据层代码

简单对象
package com.riso.junit;/*** DemoEntity* @author jie01.zhu* date 2023/10/29*/
public class DemoEntity implements java.io.Serializable {private long id;private String name;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "DemoEntity{" + "id=" + id + ", name='" + name + '\'' + '}';}}
数据层摸拟类
package com.riso.junit;import org.springframework.stereotype.Component;/*** DemoDaoImpl* @author jie01.zhu* date 2023/10/29*/
@Component
public class DemoDaoImpl {public int insert(DemoEntity demoEntity) {System.out.println("dao.insert:" + demoEntity.toString());return 1;}
}
业务逻辑层摸拟类
package com.riso.junit;import org.springframework.stereotype.Service;import javax.annotation.Resource;/*** DemoServiceImpl* @author jie01.zhu* date 2023/10/29*/
@Service
public class DemoServiceImpl {@ResourceDemoDaoImpl demoDao;public int insert(DemoEntity demoEntity) {System.out.println("service.insert:" + demoEntity.toString());return demoDao.insert(demoEntity);}}
后台任务摸拟类
package com.riso.junit;import org.springframework.stereotype.Component;/*** DemoTaskImpl* @author jie01.zhu* date 2023/10/29*/
@Component
public class DemoTaskImpl {DemoServiceImpl demoService;public int insert(DemoEntity demoEntity) {System.out.println("task.insert:" + demoEntity.toString());return demoService.insert(demoEntity);}
}

基于spring+mock+junit

maven依赖

   <!-- test --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>3.12.4</version><scope>test</scope></dependency>
package com.riso.junit.test;import com.riso.junit.DemoDaoImpl;
import com.riso.junit.DemoEntity;
import com.riso.junit.DemoServiceImpl;
import com.riso.junit.DemoTaskImpl;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;/***  junit test* @author jie01.zhu* date 2023/10/29*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}, inheritLocations = true)
public class Test1 {/*** 测试入口类*/@Resource@InjectMocksDemoTaskImpl demoTask;/*** mock的类的中间传递类*/@Resource@InjectMocksDemoServiceImpl demoService;/*** 被mock的类*/@MockDemoDaoImpl demoDao;@Testpublic void test1() {// 初始化mock环境MockitoAnnotations.openMocks(this);DemoEntity demoEntity = new DemoEntity();demoEntity.setId(1L);demoEntity.setName("name1");Mockito.doReturn(0).when(demoDao).insert(Mockito.any());int result = demoTask.insert(demoEntity);Assert.assertEquals(result, 0);}
}

基于spring+mock+testng

有二个测试类,测试参数不同,主要体现在单元测试外,控制二个测试类,按并发场景做简单的集成测试

maven依赖

        <dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>6.14.3</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><version>2.4.13</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>3.12.4</version><scope>test</scope></dependency>
package com.riso.testng.test;import com.riso.testng.ContextConfig;
import com.riso.testng.DemoDaoImpl;
import com.riso.testng.DemoEntity;
import com.riso.testng.DemoTaskImpl;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;import javax.annotation.Resource;/***  junit test* @author jie01.zhu* date 2023/10/29*/
@SpringBootTest(classes = {ContextConfig.class})
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class Test1 extends AbstractTestNGSpringContextTests {/*** 测试入口类*/@ResourceDemoTaskImpl demoTask;/*** 被mock的类 选用spy方式  默认使用原生逻辑,仅mock的方法才被mock*/@SpyBeanDemoDaoImpl demoDao;@Testpublic void test1() {// 初始化mock环境MockitoAnnotations.openMocks(this);DemoEntity demoEntity = new DemoEntity();demoEntity.setId(1L);demoEntity.setName("name1");Mockito.doReturn(0).when(demoDao).insert(Mockito.any());int result = demoTask.insert(demoEntity);Assert.assertEquals(result, 0);}
}package com.riso.testng.test;import com.riso.testng.ContextConfig;
import com.riso.testng.DemoDaoImpl;
import com.riso.testng.DemoEntity;
import com.riso.testng.DemoTaskImpl;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;import javax.annotation.Resource;/***  junit test* @author jie01.zhu* date 2023/10/29*/
@SpringBootTest(classes = {ContextConfig.class})
@TestExecutionListeners(listeners = MockitoTestExecutionListener.class)
public class Test2 extends AbstractTestNGSpringContextTests {/*** 测试入口类*/@ResourceDemoTaskImpl demoTask;/*** 被mock的类 选用spy方式  默认使用原生逻辑,仅mock的方法才被mock*/@SpyBeanDemoDaoImpl demoDao;@Testpublic void test2() {// 初始化mock环境MockitoAnnotations.openMocks(this);DemoEntity demoEntity = new DemoEntity();demoEntity.setId(2L);demoEntity.setName("name2");Mockito.doReturn(2).when(demoDao).insert(Mockito.any());int result = demoTask.insert(demoEntity);Assert.assertEquals(result, 2);}
}

testNg的 配置文件,也是执行入口

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="test" parallel="tests" thread-count="2"><test name="test1" group-by-instances="true"><classes><class name="com.riso.testng.test.Test1"/></classes></test><test name="test2" group-by-instances="true"><classes><class name="com.riso.testng.test.Test2"></class></classes></test>
</suite>

运行方式如下:
在这里插入图片描述

示例的差异点

junit与testng的主要变动不大,有以下几个点需要注意
注解部分

junit此处注解
@RunWith(SpringJUnit4ClassRunner.class)
testng不再使用此注解
需要继承 org.springframework.test.context.testng.AbstractTestNGSpringContextTests

在before,after中

testng完全兼容,但会多出Suite ,它代替xml配置中,单元测试类之上的生命周期

testng多出按配置执行功能

首先testng的单元测试可以与junit一样,单独运行
在这个基础上,也能通过testng xml按配置运行,可以见上面的例子

附上关于mock 新旧写法改进

以前要摸拟调用对象的跨二层以上类时,需要通过InjectMocks 做为中间传递,才能成功mock掉二层以上的类
换成spyBean后,不需要再使用InjectMocks ,会自动从注入中找到
这个小插曲也是我自己对以前mock的修正,一并附上

小结

通过以上说明,及示例
testng是完全兼容junit的,且改动很小
注解,断言都是直接兼容的(只需要更换导入的包路径既可)

当然,我不是为了使用而使用,一切都要建立上有需求的基础上
junit对我来讲,已经满足不了我的需求,
为了能够编写集成测试,同时复用已有的单元测试,我选择了testng
希望以上分享,可以对读都有用

朱杰
2023-10-29

相关文章:

关于测试组件junit切换testng的示例以及切换方式分享

文章目录 概要首先看看junit和testng的区别实践篇摸拟业务逻辑代码简单对象数据层摸拟类业务逻辑层摸拟类后台任务摸拟类 基于springmockjunit基于springmocktestng 示例的差异点junit与testng的主要变动不大,有以下几个点需要注意注解部分在before,after中testng多出按配置执行…...

nginx 内存管理(二)

共享内存 共享内存结构与接口定义nginx共享内存在操作系统上的兼容性设计互斥锁锁的结构体锁的一系列操作&#xff08;core/ngx_shmtx.c&#xff09;创建锁 原子操作nginx的上锁操作尝试加锁获取锁释放锁强迫解锁唤醒等待进程 slab共享内存块管理nginx的slab大小规格内存池结构…...

【DevChat】智能编程助手 - 使用评测

写在前面&#xff1a;博主是一只经过实战开发历练后投身培训事业的“小山猪”&#xff0c;昵称取自动画片《狮子王》中的“彭彭”&#xff0c;总是以乐观、积极的心态对待周边的事物。本人的技术路线从Java全栈工程师一路奔向大数据开发、数据挖掘领域&#xff0c;如今终有小成…...

Geek challenge 2023 EzHttp

打开链接需要使用post请求提交username和password 查看源码得到提示&#xff0c;爬虫想到robots协议 访问robots.txt 访问得到的路径&#xff1a;/o2takuXXs_username_and_password.txt 拿到用户名和密码&#xff1a; username:admin password:dm1N123456r00t# 进行post传参…...

matlabR2021a正版免费使用

目录 matlab介绍&#xff1a; 安装&#xff1a; matlab介绍&#xff1a; MATLAB&#xff08;Matrix Laboratory的缩写&#xff09;是一种高级技术计算和编程环境&#xff0c;由MathWorks公司开发。它在科学、工程、数据分析和数学建模领域中广泛应用&#xff0c;为用户提供了…...

天气数据可视化平台-计算机毕业设计vue

天气变幻无常&#xff0c;影响着我们生活的方方面面&#xff0c;应用天气预报信息可以及时了解天气的趋势&#xff0c;给人们的工作、生活等带来便利&#xff0c;也可以为我们为未来的事情做安排和打算&#xff0c;所以一个精准的、易读 通过利用 程序对气象网站大量的气象信息…...

揭秘Java switch语句中的case穿透现象

揭秘Java switch语句中的case穿透现象 1. switch 语句简介2. case穿透现象的原因关于 goto 3. switch和if的区别4. 总结 导语&#xff1a;在 Java 开发中&#xff0c;我们经常使用switch语句来进行条件判断和分支选择。然而&#xff0c;有一个令人困惑的现象就是&#xff0c;当…...

Java-API简析_java.io.FilterOutputStream类(基于 Latest JDK)(浅析源码)

【版权声明】未经博主同意&#xff0c;谢绝转载&#xff01;&#xff08;请尊重原创&#xff0c;博主保留追究权&#xff09; https://blog.csdn.net/m0_69908381/article/details/134106510 出自【进步*于辰的博客】 因为我发现目前&#xff0c;我对Java-API的学习意识比较薄弱…...

C语言 每日一题 PTA 10.29 day7

1.特殊a串数列求和 给定两个均不超过9的正整数a和n&#xff0c;要求编写程序求a aa aaa⋯ aa⋯a&#xff08;n个a&#xff09;之和。 输入格式&#xff1a; 输入在一行中给出不超过9的正整数a和n。 输出格式&#xff1a; 在一行中按照“s 对应的和”的格式输出。 思路 n…...

持续集成部署-k8s-服务发现-Ingress 路径匹配与虚拟主机匹配

持续集成部署-k8s-服务发现-Ingress 路径匹配与虚拟主机匹配 1. 安装 Ingress-Nginx2. 创建要代理的 Service3. 创建一个新的 Ingress-Nginx1. 安装 Ingress-Nginx 要使用 Ingress-Nginx 首先第一步是需要先安装它,安装的步骤可以参考:持续集成部署-k8s-服务发现-Ingress 2…...

selenium工作原理和反爬分析

一、 Selenium Selenium是最广泛使用的开源Web UI(用户界面)自动化测试套件之一&#xff0c;支持并行测试执行。Selenium通过使用特定于每种语言的驱动程序支持各种编程语言。Selenium支持的语言包括C#&#xff0c;Java&#xff0c;Perl&#xff0c;PHP&#xff0c;Python和Ru…...

windows电脑安装系统后固态硬盘和机械硬盘的盘符号顺序显示错乱,解决方法

一、场景 由于电脑磁盘是SSD固态硬盘自己拓展的1T机械硬盘组成&#xff0c;固态硬盘分为C、D两个盘区&#xff0c;机械硬盘分为E、F两个盘区。为了提升运行速度&#xff0c;系统安装在C盘&#xff0c;安装完成后按照习惯盘区顺应该为C、D、E、F&#xff0c;但实际情况却是D、E…...

自定义控件的子控件布局(onLayout()方法)

onLayout()方法用于指定布局中子控件的位置&#xff0c;该方法通常在自定义的ViewGroup容器中重写。 重写onLayout()方法中的常用方法&#xff1a; getChildCount() 获取子控件数量 getChildAt( int index ) 获取指定index的子控件&#xff0c;返回View view.getVisibilit…...

vscode提取扩展出错xhr

在 Visual Studio Code (VSCode) 中提取扩展出现 XHR 错误通常意味着在下载扩展或进行扩展管理操作时出现了网络请求问题。XHR (XMLHttpRequest) 是一种用于在浏览器中进行 HTTP 请求的技术&#xff0c;通常用于获取数据或资源。在 VSCode 中&#xff0c;它也可用于管理扩展的下…...

Docker 笔记(上篇)

Docker 概述 Docker 概念 Docker 是一个开源的应用容器引擎&#xff0c;让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中&#xff0c;然后发布到任何流行的 Linux或Windows操作系统的机器上&#xff0c;也可以实现虚拟化。容器是完全使用沙箱机制&#xff0c;相互之…...

python自动化测试(六):唯品会商品搜索-练习

目录 一、配置代码 二、操作 2.1 输入框“运动鞋” 2.2 点击搜索按钮 2.3 选择品牌 2.4 选择主款 2.5 适用性别 2.6 选择尺码 2.7 选择商品&#xff1a;&#xff08;通过css的属性去匹配&#xff09; 2.8 点击配送地址选项框 一、配置代码 # codingutf-8 from selen…...

深度强化学习用于博弈类游戏-基础测试与说明【1】

深度强化学习用于博弈类游戏-基础【1】 1. 强化学习方法2. 强化学习在LOL中的应⽤2.1 环境搭建2.2 游戏特征元素提取1)小地图人物位置:2)人物血量等信息3)在整个图像上寻找小兵、防御塔的位置4)自编码器提取3. 策略梯度算法简介参考资料1. 强化学习方法 伴随着人工智能的潮起…...

通过requests库使用HTTP编写的爬虫程序

使用Python的requests库可以方便地编写HTTP爬虫程序。以下是一个使用requests库的示例&#xff1a; import requests# 发送HTTP GET请求 response requests.get("http://example.com")# 检查响应状态码 if response.status_code 200:# 获取响应内容html response.…...

550MW发电机变压器组继电保护的整定计算及仿真

摘要 电力系统继电保护设计是根据系统接线图及要求选择保护方式&#xff0c;进行整定计算&#xff0c;电力系统继电保护的设计与配置是否合理直接影响到电力系统的安全运行。如果设计与配置不当&#xff0c;保护将不能正确工作&#xff0c;会扩大事故停电范围&#xff0c;造成…...

Linux 命令|服务器相关

1. 在公共 linux 上创建 python 虚拟环境 【精选】在公共Linux服务器上创建自己的python虚拟环境_服务器创建自己的环境-CSDN博客 2. 查看现存的状态&#xff0c;看有没有程序在跑 nvidia-smi命令详解-CSDN博客 3. 上传本地文件到服务器 在本地 Mac 计算机的终端中&#x…...

node 第十三天 express初见

express概念 Fast, unopinionated, minimalist web framework for Node.js 快速、独立、极简的 Node.js Web 框架。 express相当于前端的jquery, 在不更改不侵入原生node的基础上封装了大量易用且实用的服务端api, express框架的封装原理就是前面第十天我们自己封装的简易服务器…...

Python selenium模块简介

视频版教程&#xff1a;一天掌握python爬虫【基础篇】 涵盖 requests、beautifulsoup、selenium 有些网站的数据是js动态渲染的&#xff0c;我们无法通过网页源码直接找到数据&#xff0c;只能通过找接口方式来获取数据&#xff0c;但是很多时候&#xff0c;数据又是json格式的…...

DIY相机(一)libcamera库

相机选型 DIY相机首先是要确定使用的相机型号。兼容树莓派&#xff0c;画质好一些的&#xff0c;目前主要有两款&#xff1a;一是Raspberry Pi Camera Module 3&#xff0c;二是Raspberry Pi HQ Camera。 下图是Raspberry Pi Camera Module 3的相关特性。支持自动对焦和HDR等…...

PHP简单实现预定义钩子和自定义钩子

在PHP中&#xff0c;钩子&#xff08;Hooks&#xff09;是一种机制&#xff0c;允许开发人员在特定的时机插入自定义代码。通过使用钩子&#xff0c;开发人员可以在应用程序的特定事件发生时执行自定义的功能或逻辑 钩子有两种类型&#xff1a;预定义钩子和自定义钩子。 预定…...

笔记本电脑的摄像头找不到黑屏解决办法

这种问题一般来说就是缺少驱动&#xff0c;就要下载驱动。 问题&#xff1a; 解决办法&#xff1a; 1.进入联想官网下载驱动 网站&#xff1a;https://newsupport.lenovo.com.cn/driveDownloads_index.html?v9d9bc7ad5023ef3c3d5e3cf386e2f187 2.下载主机编号检测工具 3.下…...

【Git】HEAD detached from xxx 问题及解决方案

问题背景 最近用git的时候遇到了一个问题&#xff0c;场景是这样的。 我有一个分支main&#xff0c;其中有两个commit A和B&#xff0c;A是最新commit&#xff0c;B是历史commit。我先切到B看了看之前的代码&#xff0c;然后切到A&#xff0c;并进行了一些代码修改&#xff0…...

Cannot connect to the Docker

执行docker基础命令失败&#xff01; Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 原因&#xff1a;docker服务没有启动。 解决方法&#xff1a;执行 systemctl start docker 即可。...

校园物业报修小程序开发笔记一

背景 校园规模和复杂性&#xff1a; 大型学校和校园通常拥有众多的建筑物、设施和设备&#xff0c;需要有效的维护和报修系统&#xff0c;以满足学生、教职员工和校园管理人员的需求。 学生和员工需求&#xff1a; 学生和员工在校园内可能遇到各种维修问题&#xff0c;如故障的…...

用户登录前后端开发(一个简单完整的小项目)——SpringBoot与session验证(带前后端源码)全方位全流程超详细教程

&#x1f9f8;注&#xff1a;不要看我的文件多&#xff0c;那是我的其他项目&#xff0c;这个项目所用的文件我会全部用红框框起来&#xff0c;没框的部分不用管&#xff0c;前端两个文件&#xff0c;后端一个文件 &#x1f4dc; 目录 首先&#xff0c;定义前后端交互接口 然…...

FFmpeg5.1.3编译动态库踩坑之旅(基于Linux虚拟机)

准备工作 环境准备 1.Windows安装Oracle VM VirtualBox 7.0.10&#xff0c;安装ubuntu-22.04.3。 坑一&#xff1a;无法往虚拟机里拖放复制文件&#xff0c;解决办法&#xff1a;登录Ubuntu虚拟机时切换到xorg方式登录&#xff0c;参考地址&#xff1a;Ubuntu Desktop 22.04…...